-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbottomViewOfBinaryTree.java
More file actions
52 lines (42 loc) · 1.62 KB
/
bottomViewOfBinaryTree.java
File metadata and controls
52 lines (42 loc) · 1.62 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
class Solution {
// Function to return a list containing the bottom view of the given tree.
public ArrayList<Integer> bottomView(Node root) {
ArrayList<Integer> result = new ArrayList<>();
if (root == null) return result;
// Map to store the last node at each horizontal distance
TreeMap<Integer, Integer> map = new TreeMap<>();
// Queue to perform level-order traversal
Queue<Pair> queue = new LinkedList<>();
// Start with the root node at horizontal distance 0
queue.add(new Pair(root, 0));
while (!queue.isEmpty()) {
Pair current = queue.poll();
Node node = current.node;
int hd = current.hd;
// Put the node data in the map for the corresponding horizontal distance
map.put(hd, node.data);
// If there's a left child, add it to the queue with horizontal distance - 1
if (node.left != null) {
queue.add(new Pair(node.left, hd - 1));
}
// If there's a right child, add it to the queue with horizontal distance + 1
if (node.right != null) {
queue.add(new Pair(node.right, hd + 1));
}
}
// Collect the bottom view nodes from the map
for (Integer value : map.values()) {
result.add(value);
}
return result;
}
// Helper class to store a node along with its horizontal distance
class Pair {
Node node;
int hd;
Pair(Node node, int hd) {
this.node = node;
this.hd = hd;
}
}
}