0
0
DSA Javascriptprogramming~20 mins

Top View of Binary Tree in DSA Javascript - 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 output of the top view traversal for the given binary tree?
DSA Javascript
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));
A[1, 2, 3]
B[2, 1, 4, 5]
C[2, 1, 3, 5]
D[1, 2, 3, 4, 5]
Attempts:
2 left
💡 Hint
Think about horizontal distances and which nodes appear first at each horizontal distance.
🧠 Conceptual
intermediate
1:00remaining
Horizontal Distance Concept in Top View
In the top view of a binary tree, what does the horizontal distance (hd) represent?
AThe distance of a node from the root along the horizontal axis, where left child decreases hd by 1 and right child increases hd by 1
BThe depth of a node in the tree
CThe vertical level of a node from the root
DThe number of nodes in the subtree rooted at that node
Attempts:
2 left
💡 Hint
Think about how we move left or right from the root and how that affects horizontal position.
🔧 Debug
advanced
2:00remaining
Identify the Bug in Top View Implementation
What error will occur when running the following code snippet for top view traversal?
DSA Javascript
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;
}
ASyntaxError due to incorrect destructuring in the while loop
BThe output will be reversed because horizontal distances are swapped for left and right children
CTypeError because map.keys() is not iterable
DRuntime error because queue.push is called with undefined nodes
Attempts:
2 left
💡 Hint
Check how horizontal distances are assigned to left and right children.
🚀 Application
advanced
1:30remaining
Number of Nodes in Top View
Given a binary tree with 7 nodes arranged as a perfect binary tree of height 2, how many nodes will appear in its top view?
A3
B4
C7
D5
Attempts:
2 left
💡 Hint
Visualize the horizontal distances of nodes in a perfect binary tree.
Predict Output
expert
2:30remaining
Output of Top View for Complex Tree with Overlapping Nodes
What is the output of the top view traversal for the following binary tree?
DSA Javascript
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));
A[40, 20, 10, 30, 50]
B[40, 20, 10, 60, 50]
C[20, 10, 30, 50]
D[40, 20, 10, 30]
Attempts:
2 left
💡 Hint
Remember only the first node at each horizontal distance is visible from the top.