class Node { constructor(data) { this.data = data; this.left = null; this.right = null; } } function bottomView(root) { if (!root) return []; let map = new Map(); let queue = [{ node: root, hd: 0 }]; while (queue.length) { let { 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 structure: // 1 // / \ // 2 3 // \ \ // 4 5 const root = new Node(1); root.left = new Node(2); root.right = new Node(3); root.left.right = new Node(4); root.right.right = new Node(5); console.log(bottomView(root));
The bottom view is the set of nodes visible when the tree is viewed from the bottom.
Horizontal distances (hd) are assigned starting from root at 0, left child hd-1, right child hd+1.
Nodes at the same hd are overwritten as we traverse level-wise, so the last node at each hd is kept.
For this tree, bottom view nodes are at hd -1:2, 0:4, 1:3, 2:5.
class Node { constructor(data) { this.data = data; this.left = null; this.right = null; } } function bottomView(root) { if (!root) return []; let map = new Map(); let queue = [{ node: root, hd: 0 }]; while (queue.length) { let { 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 structure: // 20 // / \ // 8 22 // / \ \ // 5 3 25 // / \ // 10 14 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));
Horizontal distances (hd) are assigned from root (0), left child hd-1, right child hd+1.
Bottom view keeps the last node encountered at each hd during level order traversal.
Nodes at hd -2:5, -1:10, 0:3, 1:14, 2:25.
function bottomView(root) {
if (!root) return [];
let map = new Map();
let queue = [{ node: root, hd: 0 }];
while (queue.length) {
let { 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]);
}The code uses queue.pop() which removes from the end, making it a stack (DFS) instead of queue (BFS).
This changes the order nodes are processed, so bottom view nodes are overwritten differently.
The output will be bottom view nodes but in reversed order compared to correct BFS approach.
Horizontal distance (hd) is assigned starting from root as 0.
Left child decreases hd by 1, right child increases hd by 1.
This helps identify nodes aligned vertically when viewed from top or bottom.
class Node { constructor(data) { this.data = data; this.left = null; this.right = null; } } function bottomView(root) { if (!root) return []; let map = new Map(); let queue = [{ node: root, hd: 0 }]; while (queue.length) { let { 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 structure: // 1 // / \ // 2 3 // / \ / \ // 4 5 6 7 // \ \ // 8 9 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.right.right = new Node(8); root.right.right.right = new Node(9); console.log(bottomView(root));
Assign horizontal distances (hd) starting from root=0.
Nodes and their hd: 4(-2), 2(-1), 6(0), 8(1), 7(1), 9(2).
At hd=1, node 8 is visited after 7, so 8 overwrites 7.
Bottom view nodes from left to right: 4, 2, 6, 8, 9.