0
0
DSA Typescriptprogramming~20 mins

Top View of Binary Tree in DSA Typescript - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Top View Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Top View for a Simple Binary Tree
What is the printed top view of the binary tree after running the given code?
DSA Typescript
class Node {
  constructor(public data: number, public left: Node | null = null, public right: Node | null = null) {}
}

function topView(root: Node | null): number[] {
  if (!root) return [];
  const map = new Map<number, number>();
  const queue: Array<{node: Node; hd: number}> = [{node: root, hd: 0}];
  let minHd = 0, maxHd = 0;

  while (queue.length > 0) {
    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));
A[1, 2, 3, 4, 5]
B[2, 1, 3]
C[4, 2, 1, 3, 5]
D[2, 1, 3, 5]
Attempts:
2 left
💡 Hint
Think about horizontal distances from the root and which nodes appear first at each horizontal distance.
🧠 Conceptual
intermediate
1:30remaining
Understanding Horizontal Distance in Top View
In the top view of a binary tree, what does the 'horizontal distance' (hd) represent?
AThe relative horizontal position of a node from the root, where left child decreases hd by 1 and right child increases hd by 1.
BThe distance of a node from the root measured along the edges.
CThe depth of a node in the tree, counting edges from the root.
DThe vertical level of a node from the root, starting at 0 for the root.
Attempts:
2 left
💡 Hint
Think about how left and right children affect the horizontal position relative to the root.
🔧 Debug
advanced
2:00remaining
Identify the Bug in Top View Implementation
What error will this code produce when run, and why? function topView(root) { if (!root) return []; const map = new Map(); const queue = [{node: root, hd: 0}]; while (queue.length > 0) { const {node, hd} = queue.pop(); 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}); } return Array.from(map.values()); }
AReturns the correct top view array.
BThrows a TypeError because map is used incorrectly.
CReturns an empty array because queue.pop() removes from the end, causing incorrect traversal order.
DReturns a reversed top view array compared to expected output.
Attempts:
2 left
💡 Hint
Consider how queue.pop() affects the order of node processing compared to queue.shift().
🚀 Application
advanced
2:30remaining
Top View of a Complex Binary Tree
Given the binary tree below, what is the top view output? // Tree structure: // 10 // / \ // 5 15 // / \ \ // 3 7 18 // \ / // 4 16 Use the top view logic where the first node at each horizontal distance is included.
A[3, 5, 10, 15, 16, 18]
B[3, 5, 10, 15, 18]
C[4, 5, 10, 15, 18]
D[3, 4, 5, 10, 15, 18]
Attempts:
2 left
💡 Hint
Calculate horizontal distances for each node and pick the first node encountered at each hd in level order.
🧠 Conceptual
expert
1:30remaining
Why Use Breadth-First Search for Top View?
Why is breadth-first search (BFS) preferred over depth-first search (DFS) when computing the top view of a binary tree?
ABFS processes nodes level by level, ensuring the first node at each horizontal distance is recorded, while DFS may miss this order.
BDFS uses less memory, so it is not suitable for top view calculation.
CBFS visits nodes in random order, which helps find the top view faster.
DDFS cannot track horizontal distances, so it cannot be used for top view.
Attempts:
2 left
💡 Hint
Think about the order nodes are visited and how that affects which nodes appear in the top view.