class Node { constructor(data) { this.data = data; this.left = null; this.right = null; } } function topView(root) { if (!root) return []; const map = new Map(); const queue = [{ node: root, hd: 0 }]; let minHd = 0, maxHd = 0; while (queue.length) { const { node, hd } = queue.shift(); if (!map.has(hd)) { 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 }); minHd = Math.min(minHd, hd); maxHd = Math.max(maxHd, hd); } const result = []; for (let i = minHd; i <= maxHd; i++) { result.push(map.get(i)); } return result; } // 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(topView(root));
The top view shows nodes visible when the tree is viewed from above. We track horizontal distances (hd) from the root (hd=0). For each hd, the first node encountered in level order is included.
Here, nodes 2, 1, 3, and 5 appear at horizontal distances -1, 0, 1, and 2 respectively. Node 4 is hidden behind node 1 from the top view.
Horizontal distance (hd) is used to track the horizontal position of nodes relative to the root. Moving left decreases hd by 1, moving right increases hd by 1.
function topView(root) {
if (!root) return [];
const map = new Map();
const queue = [{ node: root, hd: 0 }];
while (queue.length) {
const { node, hd } = queue.shift();
if (!map.has(hd)) {
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 });
}
const result = [];
for (let i = Math.min(...map.keys()); i <= Math.max(...map.keys()); i++) {
result.push(map.get(i));
}
return result;
}The code assigns hd + 1 to the left child and hd - 1 to the right child, which is opposite of the correct convention. This causes the top view to be mirrored horizontally.
A perfect binary tree of height 2 has nodes at horizontal distances -2, -1, 0, 1, 2. The top view includes one node per horizontal distance, so 5 nodes appear.
class Node { constructor(data) { this.data = data; this.left = null; this.right = null; } } function topView(root) { if (!root) return []; const map = new Map(); const queue = [{ node: root, hd: 0 }]; let minHd = 0, maxHd = 0; while (queue.length) { const { node, hd } = queue.shift(); if (!map.has(hd)) { 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 }); minHd = Math.min(minHd, hd); maxHd = Math.max(maxHd, hd); } const result = []; for (let i = minHd; i <= maxHd; i++) { result.push(map.get(i)); } return result; } // Tree structure: // 10 // / \ // 20 30 // / \ \ // 40 60 50 // // Node 60 is below node 30 horizontally const root = new Node(10); root.left = new Node(20); root.right = new Node(30); root.left.left = new Node(40); root.left.right = new Node(60); root.right.right = new Node(50); console.log(topView(root));
Nodes at horizontal distances: 40(-2), 20(-1), 10(0), 30(1), 50(2). Node 60 is at hd 0 but below 10, so it is hidden.