-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path199. Binary Tree Right Side View
More file actions
44 lines (44 loc) · 1.34 KB
/
199. Binary Tree Right Side View
File metadata and controls
44 lines (44 loc) · 1.34 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
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public List<Integer> rightSideView(TreeNode root) {
// idea -
// in order to have the right side view, needs to know each level, and to know
// the most right side element, and this calls for a bfs method.
// to keep track of elements on that level, a level is also tracked
// a map is used to update the element on the right hand side
Queue<TreeNode> q = new LinkedList<>();
q.offer(root);
int lvl = 0;
TreeMap<Integer, Integer> map = new TreeMap<>();
while(!q.isEmpty())
{
int size = q.size();
while(size-- > 0)
{
TreeNode cur = q.poll();
if(cur == null) continue;
map.put(lvl, cur.val);
q.offer(cur.left);
q.offer(cur.right);
}
lvl++;
}
List<Integer> list = new ArrayList<>();
for(int k : map.keySet()) list.add(map.get(k));
return list;
}
}