-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLeetCode-199-Binary-Tree-Right-Side-View.java
More file actions
171 lines (134 loc) · 5.46 KB
/
LeetCode-199-Binary-Tree-Right-Side-View.java
File metadata and controls
171 lines (134 loc) · 5.46 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
LeetCode: https://leetcode.com/problems/binary-tree-right-side-view/
LintCode:
JiuZhang:
ProgramCreek: http://www.programcreek.com/2014/04/leetcode-binary-tree-right-side-view-java/
Analysis:
1.BFS(Two queues)
2.BFS(One queue)
3.DFS
HashMap.put(key, value), if the key is existed, the latter value will overwritten the former one
*/
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
// 1.BFS(Two queues)
// public List<Integer> rightSideView(TreeNode root) {
// List<Integer> result = new ArrayList<Integer>();
// if(root == null) return result;
// Queue<TreeNode> currLevel = new LinkedList<TreeNode>();
// currLevel.offer(root);
// while(!currLevel.isEmpty()){
// Queue<TreeNode> nextLevel = new LinkedList<TreeNode>();
// int len = currLevel.size();
// result.add(currLevel.peek().val);
// for(int i = 0; i < len; i++){
// TreeNode curr = currLevel.poll();
// if(curr.right != null) nextLevel.offer(curr.right);
// if(curr.left != null) nextLevel.offer(curr.left);
// }
// currLevel = nextLevel;
// }
// return result;
// }
// 2.BFS(One queue)
// public List<Integer> rightSideView(TreeNode root) {
// List<Integer> result = new ArrayList<Integer>();
// if(root == null) return result;
// Queue<TreeNode> queue = new LinkedList<TreeNode>();
// queue.offer(root);
// while(!queue.isEmpty()){
// int len = queue.size();
// result.add(queue.peek().val);
// for(int i = 0; i < len; i++){
// TreeNode curr = queue.poll();
// if(curr.right != null) queue.offer(curr.right);
// if(curr.left != null) queue.offer(curr.left);
// }
// }
// return result;
// }
// 1.BFS (level order traversal, using a Queue)
// public List<Integer> rightSideView(TreeNode root) {
// List<Integer> result = new ArrayList<Integer>();
// if (root == null) return result;
// Queue<TreeNode> queue = new LinkedList<>();
// queue.add(root);
// while(!queue.isEmpty()) {
// int size = queue.size();
// TreeNode curr = null;
// for (int i = 0; i < size; i++) {
// curr = queue.poll();
// if (curr.left != null) queue.add(curr.left);
// if (curr.right != null) queue.add(curr.right);
// }
// result.add(curr.val);
// }
// return result;
// }
// 3.DFS
public List<Integer> rightSideView(TreeNode root) {
List<Integer> result = new ArrayList<Integer>();
if(root == null) return result;
HashMap<Integer, Integer> depthToValue = new HashMap<Integer, Integer>();
DFS(depthToValue, root, 1);
int depth = 1;
while(depthToValue.containsKey(depth)){
result.add(depthToValue.get(depth));
depth++;
}
return result;
}
private void DFS(HashMap<Integer, Integer> depthToValue, TreeNode root, int depth){
if(root == null) return;
depthToValue.put(depth, root.val);
if(root.left != null) DFS(depthToValue, root.left, depth + 1);
if(root.right != null) DFS(depthToValue, root.right, depth + 1);
}
// 1.BFS. Both left size view and right side view.
// public List<Integer> rightSideView(TreeNode root) {
// if (root == null) return new ArrayList<>();
// List<Integer> left = new ArrayList<>();
// List<Integer> right = new ArrayList<>();
// Queue<TreeNode> queue = new LinkedList<>();
// queue.add(root);
// while(!queue.isEmpty()) {
// int size = queue.size();
// for(int i = 0; i < size; i++) {
// TreeNode curr = queue.poll();
// if (i == 0) left.add(curr.val);
// if (i == size - 1) right.add(curr.val);
// if (curr.left != null) queue.add(curr.left);
// if (curr.right != null) queue.add(curr.right);
// }
// }
// return right;
// }
// 2.BFS Recursion. Both left size view and right side view.
public List<Integer> rightSideView(TreeNode root) {
if (root == null) return new ArrayList<>();
List<Integer> left = new ArrayList<>();
List<Integer> right = new ArrayList<>();
helper(root, 0, left, right);
System.out.println(left.toString());
System.out.println(right.toString());
return right;
}
private void helper(TreeNode root, int level, List<Integer> left, List<Integer> right) {
if (left.size() <= level) left.add(level, root.val);
if (right.size() <= level) {
right.add(level, root.val);
} else {
right.set(level, root.val);
}
if (root.left != null) helper(root.left, level + 1, left, right);
if (root.right != null) helper(root.right, level + 1, left, right);
}
}