-
Notifications
You must be signed in to change notification settings - Fork 1
/
LevelOrder.java
58 lines (46 loc) · 1.2 KB
/
LevelOrder.java
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
import java.lang.*;
import java.util.*;
public class LevelOrder{
private ArrayList<ArrayList<Integer>> levelOrder(TreeNode root){
ArrayList<ArrayList<Integer>> res = new ArrayList<>();
Queue<TreeNode> queue = new LinkedList<>();
ArrayList<Integer> nodes = new ArrayList<>();
if(root == null){
return res;
}
queue.offer(root);
// mark the end of the level
queue.offer(null);
while(!queue.isEmpty()){
// dequeue the top element from the queue
TreeNode current = queue.poll();
if(current != null){
nodes.add(current.val);
if(current.left != null){
queue.offer(current.left);
}
if(current.right!= null){
queue.offer(current.right);
}
}else{
if(!queue.isEmpty()){
queue.offer(null);
}
// change the reference
res.add(nodes);
nodes = new ArrayList<>();
}
}
return res;
}
public static void main(String [] args){
TreeNode root = new TreeNode(1);
root.left = new TreeNode(2);
root.right = new TreeNode(3);
root.left.left = new TreeNode(4);
root.left.right = new TreeNode(5);
root.right.left = new TreeNode(6);
ArrayList<ArrayList<Integer>> res = new LevelOrder().levelOrder(root);
System.out.println(res);
}
}