-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuestion102.java
More file actions
66 lines (56 loc) · 1.74 KB
/
Copy pathQuestion102.java
File metadata and controls
66 lines (56 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import java.util.*;
public class Question102
{
public static void getVal(List<List<Integer>> result,int level, TreeNode root)
{
List<Integer> temp = result.get(level);
temp.add(root.val);
if(root.left!=null)
getVal(result,level+1,root.left);
if(root.right!=null)
getVal(result,level+1,root.right);
}
public static List<List<Integer>> levelOrder(TreeNode root)
{
List<List<Integer>> result = new ArrayList<>();
if(root == null)
return result;
//Find depth
int treeDepth = depth(root);
for(int i = 0;i<treeDepth;i++)
{
List<Integer> temp = new ArrayList<>();
result.add(temp);
}
getVal(result,0,root);
return result;
}
public static int depth(TreeNode root)
{
if (root == null)
return 0;
int lDepth = depth(root.left);
int rDepth = depth(root.right);
if(lDepth>rDepth)
return lDepth + 1;
else
return rDepth + 1;
}
public static void main(String[] args)
{
TreeNode root = new TreeNode(10,null,null);
TreeNode l1 = new TreeNode(1,null,null);
TreeNode r1 = new TreeNode(3,null,null);
TreeNode ll2 = new TreeNode(-2,null,null);
TreeNode rr2 = new TreeNode(20,null,null);
TreeNode last = new TreeNode(33,null,null);
root.left = l1;
root.right = r1;
l1.left = ll2;
r1.right = rr2;
rr2.right = last;
Trees.display(root);
List<List<Integer>> result = levelOrder(root);
System.out.println(result);
}
}