-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinaryTreeLevelOrderTraversal.java
49 lines (48 loc) · 1.55 KB
/
BinaryTreeLevelOrderTraversal.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
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
/**
* Created by JiahengYu on 08/06/15.
*/
public class BinaryTreeLevelOrderTraversal {
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
}
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> result=new LinkedList<List<Integer>>();
if (root==null)
return result;
Queue<TreeNode> one=new ConcurrentLinkedQueue<TreeNode>();
Queue<TreeNode> two=new ConcurrentLinkedQueue<TreeNode>();
one.add(root);
while (!one.isEmpty()||!two.isEmpty()){
List<Integer> temp1=new LinkedList<Integer>();
while (!one.isEmpty()){
TreeNode temp=one.poll();
temp1.add(temp.val);
if(temp.left!=null)
two.add(temp.left);
if(temp.right!=null)
two.add(temp.right);
}
if(temp1.size()>0)
result.add(temp1);
List<Integer> temp2=new LinkedList<Integer>();
while (!two.isEmpty()){
TreeNode temp=two.poll();
temp2.add(temp.val);
if(temp.left!=null)
one.add(temp.left);
if(temp.right!=null)
one.add(temp.right);
}
if(temp2.size()>0)
result.add(temp2);
}
return result;
}
}