Challenge - 5 Problems
Vertical Order Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Vertical Order Traversal for a Simple Tree
What is the output of the vertical order traversal for the following binary tree?
Tree structure:
1
/ \ 2 3
/ \ 4 5
Assume vertical order traversal groups nodes by their horizontal distance from root, from left to right, and within each group nodes are listed top to bottom.
Tree structure:
1
/ \ 2 3
/ \ 4 5
Assume vertical order traversal groups nodes by their horizontal distance from root, from left to right, and within each group nodes are listed top to bottom.
DSA Javascript
class Node { constructor(val, left = null, right = null) { this.val = val; this.left = left; this.right = right; } } function verticalOrderTraversal(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, []); map.get(hd).push(node.val); 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; } const root = new Node(1, new Node(2), new Node(3, new Node(4), new Node(5))); console.log(verticalOrderTraversal(root));
Attempts:
2 left
💡 Hint
Think about horizontal distances: left child decreases hd by 1, right child increases hd by 1.
✗ Incorrect
Nodes are grouped by horizontal distance (hd). Node 2 is at hd -1, node 1 and 4 at hd 0, node 3 at hd 1, and node 5 at hd 2. So the vertical order is [[2], [1, 4], [3], [5]].
🧠 Conceptual
intermediate1:00remaining
Understanding Horizontal Distance in Vertical Order Traversal
In vertical order traversal of a binary tree, what does the horizontal distance (hd) represent?
Attempts:
2 left
💡 Hint
Think about how left and right children affect horizontal position.
✗ Incorrect
Horizontal distance is a measure of how far left or right a node is from the root. Left child decreases hd by 1, right child increases hd by 1.
❓ Predict Output
advanced2:30remaining
Output of Vertical Order Traversal with Multiple Nodes at Same Position
What is the output of the vertical order traversal for the following binary tree?
Tree structure:
1
/ \ > 2 3
/ \ \ > 4 5 6
\
7
Nodes at the same horizontal distance and level should be listed in order of appearance from left to right.
Tree structure:
1
/ \ > 2 3
/ \ \ > 4 5 6
\
7
Nodes at the same horizontal distance and level should be listed in order of appearance from left to right.
DSA Javascript
class Node { constructor(val, left = null, right = null) { this.val = val; this.left = left; this.right = right; } } function verticalOrderTraversal(root) { if (!root) return []; const map = new Map(); const queue = [{ node: root, hd: 0, level: 0 }]; let minHd = 0, maxHd = 0; while (queue.length) { const { node, hd, level } = queue.shift(); if (!map.has(hd)) map.set(hd, []); map.get(hd).push({ val: node.val, level }); if (node.left) queue.push({ node: node.left, hd: hd - 1, level: level + 1 }); if (node.right) queue.push({ node: node.right, hd: hd + 1, level: level + 1 }); minHd = Math.min(minHd, hd); maxHd = Math.max(maxHd, hd); } const result = []; for (let i = minHd; i <= maxHd; i++) { const arr = map.get(i); arr.sort((a, b) => a.level - b.level); result.push(arr.map(x => x.val)); } return result; } const root = new Node(1, new Node(2, new Node(4), new Node(5, null, new Node(7))), new Node(3, null, new Node(6))); console.log(verticalOrderTraversal(root));
Attempts:
2 left
💡 Hint
Remember to sort nodes by their level within each horizontal distance group.
✗ Incorrect
At hd=0, nodes 1 (level 0), 5 (level 2), and 7 (level 3) appear. They are sorted by level: 1, 5, then 7. Other groups follow similarly.
🔧 Debug
advanced2:00remaining
Identify the Error in Vertical Order Traversal Code
The following code attempts to perform vertical order traversal but produces incorrect output. What is the main error?
Code snippet:
Code snippet:
function verticalOrderTraversal(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, []);
map.get(hd).push(node.val);
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());
}Attempts:
2 left
💡 Hint
Check how horizontal distance changes for left and right children.
✗ Incorrect
Left child should have hd - 1, right child hd + 1. The code swaps these, causing incorrect grouping.
🚀 Application
expert1:30remaining
Number of Vertical Lines in a Complex Binary Tree
Given the binary tree below, how many vertical lines (unique horizontal distances) will the vertical order traversal produce?
Tree structure:
10
/ \ > 7 15
/ \ \ > 5 8 20
/ / \ > 3 17 25
\ > 18
Count the number of unique horizontal distances from the root node.
Tree structure:
10
/ \ > 7 15
/ \ \ > 5 8 20
/ / \ > 3 17 25
\ > 18
Count the number of unique horizontal distances from the root node.
Attempts:
2 left
💡 Hint
Track horizontal distance changes from root to each node.
✗ Incorrect
Horizontal distances range from -3 (node 3) to +3 (node 25), inclusive, totaling 7 unique vertical lines.