-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path199_BinaryTreeRightView.java
64 lines (58 loc) · 1.74 KB
/
199_BinaryTreeRightView.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
// Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom.
// For example:
// Given the following binary tree,
// 1 <---
// / \
// 2 3 <---
// \ \
// 5 4 <---
// You should return [1, 3, 4].
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
//smart
public class Solution {
public List<Integer> rightSideView(TreeNode root) {
List<Integer> res = new ArrayList<>();
helper(res, root, 0);
return res;
}
public void helper(List<Integer> res, TreeNode root, int level) {
if(root == null) return;
if(level == res.size()) {
res.add(root.val);
}
helper(res, root.right, level + 1);
helper(res, root.left, level + 1);
}
}
//modification of zigzag
public class Solution {
public List<Integer> rightSideView(TreeNode root) {
List<List<Integer>> res = new ArrayList<List<Integer>>();
helper(res, root, 0);
List<Integer> list = new ArrayList<>();
for(int i=0; i<res.size(); i++) {
List<Integer> temp = res.get(i);
int cur = temp.get(temp.size() - 1);
list.add(cur);
}
return list;
}
public void helper(List<List<Integer>> res, TreeNode root, int level) {
if(root == null) return;
if(res.size() <= level) {
res.add(new ArrayList<Integer>());
}
List<Integer> list = res.get(level);
list.add(root.val);
helper(res, root.left, level + 1);
helper(res, root.right, level + 1);
}
}