-
Notifications
You must be signed in to change notification settings - Fork 1
/
bottom-view-of-binary-tree.java
41 lines (33 loc) · 1.28 KB
/
bottom-view-of-binary-tree.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
class Solution {
public ArrayList<Integer> bottomView(Node root) {
ArrayList<Integer> result = new ArrayList<>();
if (root == null) return result;
// TreeMap to store the last node at each horizontal distance
TreeMap<Integer, Integer> map = new TreeMap<>();
Queue<Node> queue = new LinkedList<>();
// Initialize the queue with the root node and horizontal distance 0
root.hd = 0;
queue.add(root);
while (!queue.isEmpty()) {
Node temp = queue.poll();
int hd = temp.hd;
// Overwrite the data at the current horizontal distance in the map
map.put(hd, temp.data);
// If there's a left child, add it to the queue with hd - 1
if (temp.left != null) {
temp.left.hd = hd - 1;
queue.add(temp.left);
}
// If there's a right child, add it to the queue with hd + 1
if (temp.right != null) {
temp.right.hd = hd + 1;
queue.add(temp.right);
}
}
// Add the bottom view nodes to the result list
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
result.add(entry.getValue());
}
return result;
}
}