0
0
DSA Javascriptprogramming~10 mins

Vertical Order Traversal of Binary Tree in DSA Javascript - Interactive Practice

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

Complete the code to initialize the map that stores nodes by their vertical column.

DSA Javascript
const columnTable = new Map();

function verticalOrder(root) {
  if (!root) return [];
  const queue = [[root, 0]];
  while (queue.length > 0) {
    const [node, column] = queue.shift();
    if (!columnTable.has([1])) {
      columnTable.set(column, []);
    }
    columnTable.get(column).push(node.val);
    if (node.left) queue.push([node.left, column - 1]);
    if (node.right) queue.push([node.right, column + 1]);
  }
  return Array.from(columnTable.keys()).sort((a, b) => a - b).map(key => columnTable.get(key));
}
Drag options to blanks, or click blank then click option'
Aqueue
Bnode
Ccolumn
Droot
Attempts:
3 left
💡 Hint
Common Mistakes
Checking for the node object instead of the column number.
Using the root or queue variable incorrectly.
2fill in blank
medium

Complete the code to add the left child node to the queue with the correct column index.

DSA Javascript
if (node.left) queue.push([node.left, [1]]);
Drag options to blanks, or click blank then click option'
A0
Bcolumn + 1
Ccolumn
Dcolumn - 1
Attempts:
3 left
💡 Hint
Common Mistakes
Adding 1 instead of subtracting 1 for left child.
Using the same column index for left child.
3fill in blank
hard

Fix the error in the sorting function to correctly sort the column keys numerically.

DSA Javascript
return Array.from(columnTable.keys()).sort((a, b) => [1]).map(key => columnTable.get(key));
Drag options to blanks, or click blank then click option'
Aa - b
Bb - a
Ca + b
Da * b
Attempts:
3 left
💡 Hint
Common Mistakes
Using addition or multiplication which does not sort correctly.
Reversing the order by subtracting a from b.
4fill in blank
hard

Fill both blanks to correctly add the right child node to the queue with the updated column index.

DSA Javascript
if (node.right) queue.push([[1], [2]]);
Drag options to blanks, or click blank then click option'
Anode.right
Bcolumn + 1
Cnode.left
Dcolumn - 1
Attempts:
3 left
💡 Hint
Common Mistakes
Using node.left instead of node.right.
Using column - 1 instead of column + 1 for right child.
5fill in blank
hard

Fill all three blanks to create a map of columns to sorted node values after traversal.

DSA Javascript
const sortedColumns = Array.from(columnTable.keys()).sort(([1], [2]) => [3]);
const result = sortedColumns.map(key => columnTable.get(key).sort((a, b) => a - b));
Drag options to blanks, or click blank then click option'
Aa
Bb
Ca - b
Db - a
Attempts:
3 left
💡 Hint
Common Mistakes
Using b - a which sorts descending.
Mixing parameter names or using incorrect subtraction.