0
0
DSA Typescriptprogramming~10 mins

Top View of Binary Tree in DSA Typescript - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a binary tree node class with left and right children.

DSA Typescript
class Node {
  value: number;
  left: Node | null;
  right: Node | null;
  constructor(value: number) {
    this.value = value;
    this.left = [1];
    this.right = null;
  }
}
Drag options to blanks, or click blank then click option'
A0
Bundefined
Cnull
Dfalse
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 or false instead of null for child nodes.
Leaving the child uninitialized.
2fill in blank
medium

Complete the code to insert a node into the binary tree at the left child.

DSA Typescript
function insertLeft(parent: Node, value: number): void {
  parent.left = new Node([1]);
}
Drag options to blanks, or click blank then click option'
Avalue
Bparent.value
Cnull
Dundefined
Attempts:
3 left
💡 Hint
Common Mistakes
Using parent.value instead of the new value.
Assigning null or undefined instead of a new node.
3fill in blank
hard

Fix the error in the function to compute the top view by completing the missing code to track horizontal distance.

DSA Typescript
function topView(root: Node): number[] {
  if (!root) return [];
  const queue: Array<[Node, number]> = [[root, 0]];
  const map = new Map<number, number>();
  while (queue.length > 0) {
    const [node, hd] = queue.shift()!;
    if (!map.has([1])) {
      map.set(hd, node.value);
    }
    if (node.left) queue.push([node.left, hd - 1]);
    if (node.right) queue.push([node.right, hd + 1]);
  }
  const sortedKeys = Array.from(map.keys()).sort((a, b) => a - b);
  const result = sortedKeys.map(key => map.get(key)!);
  return result;
}
Drag options to blanks, or click blank then click option'
Aqueue.length
Bhd
Cnode.value
Dnode.left
Attempts:
3 left
💡 Hint
Common Mistakes
Checking node.value instead of hd in the map.
Using queue length or node.left as key mistakenly.
4fill in blank
hard

Fill both blanks to correctly update the queue with child nodes and their horizontal distances.

DSA Typescript
if (node.left) queue.push([node.left, [1]]);
if (node.right) queue.push([node.right, [2]]);
Drag options to blanks, or click blank then click option'
Ahd - 1
Bhd + 1
Chd
Dnode.value
Attempts:
3 left
💡 Hint
Common Mistakes
Using the same horizontal distance for both children.
Using node.value instead of hd adjustments.
5fill in blank
hard

Fill all three blanks to return the top view values sorted by horizontal distance.

DSA Typescript
const sortedKeys = Array.from(map.keys()).sort((a, b) => [1]);
const result = sortedKeys.map(key => map.get([2])!);
return [3];
Drag options to blanks, or click blank then click option'
Aa - b
Bkey
Cresult
Db - a
Attempts:
3 left
💡 Hint
Common Mistakes
Sorting keys descending (b - a) instead of ascending.
Returning keys instead of mapped values.
Using wrong variable names.