-
Notifications
You must be signed in to change notification settings - Fork 34
/
Solution.java
36 lines (36 loc) · 977 Bytes
/
Solution.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
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public List<Integer> rightSideView(TreeNode root) {
List<Integer> answer=new ArrayList<Integer>();
if (root==null) {
return answer;
}
List<TreeNode> list=new ArrayList<TreeNode>();
list.add(root);
int k=list.size();
while (k>0) {
answer.add(list.get(list.size()-1).val);
for (int i = 0; i < k; i++) {
if (list.get(i).left!=null) {
list.add(list.get(i).left);
}
if (list.get(i).right!=null) {
list.add(list.get(i).right);
}
list.remove(i);
i--;
k--;
}
k=list.size();
}
return answer;
}
}