0
0
DSA Typescriptprogramming~20 mins

Vertical Order Traversal of Binary Tree in DSA Typescript - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Vertical Order Traversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Vertical Order Traversal for a Simple Tree
What is the vertical order traversal output of the following binary tree?

Tree structure:
1
/ \
2 3
/ \
4 5
DSA Typescript
class TreeNode {
  val: number;
  left: TreeNode | null;
  right: TreeNode | null;
  constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
    this.val = val === undefined ? 0 : val;
    this.left = left === undefined ? null : left;
    this.right = right === undefined ? null : right;
  }
}

function verticalOrderTraversal(root: TreeNode | null): number[][] {
  if (!root) return [];
  const map = new Map<number, number[]>();
  const queue: Array<[TreeNode, number]> = [[root, 0]];
  let min = 0, max = 0;

  while (queue.length) {
    const [node, col] = queue.shift()!;
    if (!map.has(col)) map.set(col, []);
    map.get(col)!.push(node.val);
    if (node.left) queue.push([node.left, col - 1]);
    if (node.right) queue.push([node.right, col + 1]);
    min = Math.min(min, col);
    max = Math.max(max, col);
  }

  const result: number[][] = [];
  for (let i = min; i <= max; i++) {
    result.push(map.get(i)!);
  }
  return result;
}

const root = new TreeNode(1, new TreeNode(2), new TreeNode(3, new TreeNode(4), new TreeNode(5)));
console.log(verticalOrderTraversal(root));
A[[2],[4,1],[3],[5]]
B[[2],[1],[4,3],[5]]
C[[2],[1,4],[3],[5]]
D[[1],[2,3],[4,5]]
Attempts:
2 left
💡 Hint
Think about columns from left to right and nodes in each column by level order.
Predict Output
intermediate
2:00remaining
Vertical Order Traversal Output with Multiple Nodes in Same Column and Row
What is the vertical order traversal output of this binary tree?

Tree structure:
3
/ \
9 8
/ \ / \
4 0 1 7
DSA Typescript
class TreeNode {
  val: number;
  left: TreeNode | null;
  right: TreeNode | null;
  constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
    this.val = val === undefined ? 0 : val;
    this.left = left === undefined ? null : left;
    this.right = right === undefined ? null : right;
  }
}

function verticalOrderTraversal(root: TreeNode | null): number[][] {
  if (!root) return [];
  const map = new Map<number, number[]>();
  const queue: Array<[TreeNode, number]> = [[root, 0]];
  let min = 0, max = 0;

  while (queue.length) {
    const [node, col] = queue.shift()!;
    if (!map.has(col)) map.set(col, []);
    map.get(col)!.push(node.val);
    if (node.left) queue.push([node.left, col - 1]);
    if (node.right) queue.push([node.right, col + 1]);
    min = Math.min(min, col);
    max = Math.max(max, col);
  }

  const result: number[][] = [];
  for (let i = min; i <= max; i++) {
    result.push(map.get(i)!);
  }
  return result;
}

const root = new TreeNode(3, new TreeNode(9, new TreeNode(4), new TreeNode(0)), new TreeNode(8, new TreeNode(1), new TreeNode(7)));
console.log(verticalOrderTraversal(root));
A[[4],[9,0],[3,1],[8],[7]]
B[[4],[9],[3,0,1],[8],[7]]
C[[4],[9],[3,1,0],[8],[7]]
D[[4],[9],[3,0],[8,1],[7]]
Attempts:
2 left
💡 Hint
Nodes in the same column are listed in order of their level from top to bottom.
🔧 Debug
advanced
2:00remaining
Identify the Error in Vertical Order Traversal Implementation
What error will this code produce when run on a non-empty binary tree?
DSA Typescript
function verticalOrderTraversal(root) {
  if (!root) return [];
  const map = new Map();
  const queue = [[root, 0]];
  let min = 0, max = 0;

  while (queue.length) {
    const [node, col] = queue.pop();
    if (!map.has(col)) map.set(col, []);
    map.get(col).push(node.val);
    if (node.left) queue.push([node.left, col - 1]);
    if (node.right) queue.push([node.right, col + 1]);
    min = Math.min(min, col);
    max = Math.max(max, col);
  }

  const result = [];
  for (let i = min; i <= max; i++) {
    result.push(map.get(i));
  }
  return result;
}
AThe traversal order is incorrect because queue.pop() removes from the end, causing depth-first behavior instead of breadth-first.
BNo error; the code runs correctly and returns vertical order traversal.
CTypeError because map.get(col) returns undefined and push is called on undefined.
DSyntaxError due to missing semicolon after map.set(col, []).
Attempts:
2 left
💡 Hint
Consider how queue.pop() affects the order of node processing compared to queue.shift().
🧠 Conceptual
advanced
1:30remaining
Understanding Column Indexing in Vertical Order Traversal
In vertical order traversal, what does the column index represent and how is it assigned to nodes?
AColumn index represents horizontal distance from root; root is 0, left child is parent column -1, right child is parent column +1.
BColumn index represents depth level; root is 0, children increment column by 1 regardless of side.
CColumn index is assigned randomly to nodes to group them vertically.
DColumn index counts nodes from left to right in the same level, starting at 0.
Attempts:
2 left
💡 Hint
Think about how horizontal position changes when moving left or right in the tree.
🚀 Application
expert
3:00remaining
Find the Vertical Order Traversal Output for Complex Tree
Given the binary tree below, what is the vertical order traversal output?

Tree structure:
10
/ \
5 15
/ \ \
3 7 18
\ /
4 16
DSA Typescript
class TreeNode {
  val: number;
  left: TreeNode | null;
  right: TreeNode | null;
  constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
    this.val = val === undefined ? 0 : val;
    this.left = left === undefined ? null : left;
    this.right = right === undefined ? null : right;
  }
}

function verticalOrderTraversal(root: TreeNode | null): number[][] {
  if (!root) return [];
  const map = new Map<number, number[]>();
  const queue: Array<[TreeNode, number]> = [[root, 0]];
  let min = 0, max = 0;

  while (queue.length) {
    const [node, col] = queue.shift()!;
    if (!map.has(col)) map.set(col, []);
    map.get(col)!.push(node.val);
    if (node.left) queue.push([node.left, col - 1]);
    if (node.right) queue.push([node.right, col + 1]);
    min = Math.min(min, col);
    max = Math.max(max, col);
  }

  const result: number[][] = [];
  for (let i = min; i <= max; i++) {
    result.push(map.get(i)!);
  }
  return result;
}

const root = new TreeNode(10,
  new TreeNode(5, new TreeNode(3, null, new TreeNode(4)), new TreeNode(7)),
  new TreeNode(15, null, new TreeNode(18, new TreeNode(16), null))
);
console.log(verticalOrderTraversal(root));
A[[3],[5,4],[10,7,16],[15],[18]]
B[[3],[5],[10,4,7],[15,16],[18]]
C[[3,4],[5],[10,7],[15,16],[18]]
D[[3],[5,4],[10,7],[15,16],[18]]
Attempts:
2 left
💡 Hint
Track column indices carefully and list nodes in each column by their level order.