class Node { constructor(public data: number, public left: Node | null = null, public right: Node | null = null) {} } function bottomView(root: Node | null): number[] { if (!root) return []; const map = new Map<number, number>(); const queue: Array<{node: Node; hd: number}> = [{node: root, hd: 0}]; while (queue.length > 0) { const {node, hd} = queue.shift()!; map.set(hd, node.data); if (node.left) queue.push({node: node.left, hd: hd - 1}); if (node.right) queue.push({node: node.right, hd: hd + 1}); } return Array.from(map.entries()) .sort((a, b) => a[0] - b[0]) .map(entry => entry[1]); } // Tree construction const root = new Node(20); root.left = new Node(8); root.right = new Node(22); root.left.left = new Node(5); root.left.right = new Node(3); root.right.right = new Node(25); root.left.right.left = new Node(10); root.left.right.right = new Node(14); console.log(bottomView(root));
The bottom view includes nodes visible when the tree is viewed from the bottom. The horizontal distances (hd) are used to track nodes. The last node encountered at each hd is stored.
For this tree, the bottom view nodes by hd are: hd=-2:5, hd=-1:10, hd=0:3, hd=1:14, hd=2:25.
Horizontal distance is a measure of how far left or right a node is from the root node. The root has hd=0, left child decreases hd by 1, right child increases hd by 1.
function bottomView(root: Node | null): number[] {
if (!root) return [];
const map = new Map<number, number>();
const queue: Array<{node: Node; hd: number}> = [{node: root, hd: 0}];
while (queue.length > 0) {
const {node, hd} = queue.pop()!;
map.set(hd, node.data);
if (node.left) queue.push({node: node.left, hd: hd - 1});
if (node.right) queue.push({node: node.right, hd: hd + 1});
}
return Array.from(map.entries())
.sort((a, b) => a[0] - b[0])
.map(entry => entry[1]);
}Using pop() makes the queue behave like a stack (LIFO), which changes the traversal order from level order to depth-first. This causes the bottom view to be incorrect.
class Node { constructor(public data: number, public left: Node | null = null, public right: Node | null = null) {} } function bottomView(root: Node | null): number[] { if (!root) return []; const map = new Map<number, number>(); const queue: Array<{node: Node; hd: number}> = [{node: root, hd: 0}]; while (queue.length > 0) { const {node, hd} = queue.shift()!; map.set(hd, node.data); if (node.left) queue.push({node: node.left, hd: hd - 1}); if (node.right) queue.push({node: node.right, hd: hd + 1}); } return Array.from(map.entries()) .sort((a, b) => a[0] - b[0]) .map(entry => entry[1]); } const root = new Node(1); root.left = new Node(2); root.right = new Node(3); root.left.right = new Node(4); root.right.left = new Node(5); root.right.right = new Node(6); root.right.left.left = new Node(7); root.right.left.right = new Node(8); console.log(bottomView(root));
Horizontal distances and last nodes at each hd: hd=-1:7 (2 overwritten by 7), hd=0:5 (1 overwritten by 4 then 5), hd=1:8 (3 overwritten by 8), hd=2:6.
Sorted by hd ascending: [7, 5, 8, 6].
class Node { constructor(public data: number, public left: Node | null = null, public right: Node | null = null) {} } function bottomView(root: Node | null): number[] { if (!root) return []; const map = new Map<number, number>(); const queue: Array<{node: Node; hd: number}> = [{node: root, hd: 0}]; while (queue.length > 0) { const {node, hd} = queue.shift()!; map.set(hd, node.data); if (node.left) queue.push({node: node.left, hd: hd - 1}); if (node.right) queue.push({node: node.right, hd: hd + 1}); } return Array.from(map.entries()) .sort((a, b) => a[0] - b[0]) .map(entry => entry[1]); } const root = new Node(1); root.left = new Node(2); root.right = new Node(3); root.left.left = new Node(4); root.left.right = new Node(5); root.right.left = new Node(6); root.right.right = new Node(7); root.left.left.right = new Node(8); root.right.left.left = new Node(9); console.log(bottomView(root));
Horizontal distances and last nodes: hd=-2:4, hd=-1:9 (2 overwritten by 8 then 9), hd=0:6 (1 overwritten by 5 then 6), hd=1:3, hd=2:7.
Sorted by hd ascending: [4, 9, 6, 3, 7].