-
Notifications
You must be signed in to change notification settings - Fork 0
/
CN Bottom View Of Binary Tree - iterative.java
51 lines (42 loc) · 1.51 KB
/
CN Bottom View Of Binary Tree - iterative.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
/*********************************************
Following is the TreeNode class structure
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int val) {
this.val = val;
this.left = null;
this.right = null;
}
}
*********************************************/
import java.util.*;
public class Solution {
public static class Element {
int hd = 0;
TreeNode node;
Element(int hd, TreeNode node) {
this.hd = hd;
this.node = node;
}
}
public static List<Integer> bottomView(TreeNode root) {
// Write your code here.
if (root == null) return new ArrayList<>();
Queue<Element> q = new LinkedList<>();
TreeMap<Integer, Integer> map = new TreeMap<>();
q.offer(new Element(0, root));
while (!q.isEmpty()) {
Element e = q.poll();
int hd = e.hd;
TreeNode cur = e.node;
map.put(hd, cur.val);
if (cur.left != null) q.offer(new Element(hd - 1, cur.left));
if (cur.right != null) q.offer(new Element(hd + 1, cur.right));
}
List<Integer> ans = new ArrayList<>(map.values());
return ans;
}
}
// This uses a level order traversal so, for each horizontal distance the val in map will be the last updated values ie the last level at each hd. (No need to take in count the vertical height at the latest value will always have a greater height).