-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinaryTreeRightSideView2.java
71 lines (69 loc) · 2.01 KB
/
BinaryTreeRightSideView2.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
59
60
61
62
63
64
65
66
67
68
69
70
71
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
/**
* Created by JiahengYu on 16/06/15.
*/
public class BinaryTreeRightSideView2 {
static class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
}
public List<Integer> rightSideView(TreeNode root) {
List<Integer> list=new LinkedList<Integer>();
if (root==null)
return list;
Queue<TreeNode> one = new ConcurrentLinkedQueue<TreeNode>();
Queue<TreeNode> two = new ConcurrentLinkedQueue<TreeNode>();
one.add(root);
while (!one.isEmpty()||!two.isEmpty()){
int i=0;
while (!one.isEmpty()){
TreeNode temp=one.poll();
if (i==0) {
list.add(temp.val);
}
if (temp.right!=null) {
two.add(temp.right);
}
if (temp.left!=null){
two.add(temp.left);
}
i++;
}
i=0;
while (!two.isEmpty()){
TreeNode temp=two.poll();
if (i==0) {
list.add(temp.val);
}
if (temp.right!=null) {
one.add(temp.right);
}
if (temp.left!=null){
one.add(temp.left);
}
i++;
}
}
return list;
}
public static void main(String args[]){
TreeNode t1=new TreeNode(1);
TreeNode t2=new TreeNode(2);
// TreeNode t3=new TreeNode(3);
// TreeNode t4=new TreeNode(4);
// TreeNode t5=new TreeNode(5);
t1.left=t2;
// t1.right=t3;
// t2.right=t5;
// t3.right=t4;
List<Integer> result=new BinaryTreeRightSideView2().rightSideView(t1);
for (int a:result){
System.out.print(a);
}
}
}