-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathright_side_binary_tree.java
More file actions
47 lines (41 loc) · 1.13 KB
/
Copy pathright_side_binary_tree.java
File metadata and controls
47 lines (41 loc) · 1.13 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
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List<Integer> rightSideView(TreeNode root) {
Queue<TreeNode> q =new LinkedList<TreeNode>();
q.add(null);
q.add(root);
List<Integer> res = new ArrayList<Integer>();
while(!q.isEmpty())
{
if(q.peek()==null)
{
q.remove();
TreeNode cur = q.poll();
if(cur==null)
continue;
res.add(cur.val);
q.add(null);
if(cur.right!=null)
q.add(cur.right);
if(cur.left!=null)
q.add(cur.left);
continue;
}
//System.out.println(res);
TreeNode cur = q.poll();
if(cur.right!=null)
q.add(cur.right);
if(cur.left!=null)
q.add(cur.left);
}
return res;
}
}