Challenge - 5 Problems
Vertical Order Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput 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
Tree structure:
1
/ \
2 3
/ \
4 5
DSA Java
class TreeNode { int val; TreeNode left, right; TreeNode(int v) { val = v; } } // Vertical order traversal method returns list of lists of integers // Each inner list contains nodes in one vertical column from left to right // Assume verticalOrder(root) returns List<List<Integer>> // For this tree, what is the vertical order traversal output? // Output format: [[2], [1, 4], [3], [5]]
Attempts:
2 left
💡 Hint
Think about columns from left to right and nodes in each column top to bottom.
✗ Incorrect
Vertical order groups nodes by their horizontal distance from root. Left child is one column left (-1), right child one column right (+1). Nodes in same column are listed top to bottom.
🧠 Conceptual
intermediateUnderstanding Horizontal Distance in Vertical Order Traversal
In vertical order traversal, what does the 'horizontal distance' of a node represent?
Attempts:
2 left
💡 Hint
Think about how columns are assigned in vertical order traversal.
✗ Incorrect
Horizontal distance is a number assigned to each node based on its position relative to the root: root is 0, left child is parent minus 1, right child is parent plus 1.
❓ Predict Output
advancedOutput of Vertical Order Traversal with Multiple Nodes in Same Column and Row
Given the binary tree below, what is the vertical order traversal output?
Tree structure:
1
/ \
2 3
/ \ \
4 5 6
\
7
Tree structure:
1
/ \
2 3
/ \ \
4 5 6
\
7
DSA Java
/*
Vertical order traversal groups nodes by horizontal distance.
Nodes in same column are listed top to bottom.
If nodes share same row and column, order by node value ascending.
*/Attempts:
2 left
💡 Hint
Remember to order nodes with same row and column by their value.
✗ Incorrect
Column -2: 4
Column -1: 2
Column 0: 1 (row 0), 5 (row 2)
Column 1: 3 (row 1), 7 (row 3)
Column 2: 6
Column -1: 2
Column 0: 1 (row 0), 5 (row 2)
Column 1: 3 (row 1), 7 (row 3)
Column 2: 6
🔧 Debug
advancedIdentify the Error in Vertical Order Traversal Code Snippet
What error will this Java code snippet cause when performing vertical order traversal?
Code snippet:
```java Map> map = new TreeMap<>();
Queue queue = new LinkedList<>();
Queue cols = new LinkedList<>();
queue.add(root);
cols.add(0);
while (!queue.isEmpty()) {
TreeNode node = queue.poll();
int col = cols.poll();
if (!map.containsKey(col)) {
map.put(col, new ArrayList<>());
}
map.get(col).add(node.val);
if (node.left != null) {
queue.add(node.left);
cols.add(col - 1);
}
if (node.right != null) {
queue.add(node.right);
cols.add(col + 1);
}
}
return map.values();
```
Code snippet:
```java Map
Attempts:
2 left
💡 Hint
Consider what happens if root is null before adding to queue.
✗ Incorrect
The code lacks a null check for root before adding it to the queue. If root is null, queue.add(null) succeeds, but queue.poll() returns null, causing NullPointerException when accessing node.val.
🚀 Application
expertNumber of Vertical Columns in a Complex Binary Tree
Consider a binary tree where each node has either 0 or 2 children, and the tree is perfectly balanced with height 3 (root at height 0). How many vertical columns will the vertical order traversal produce?
Attempts:
2 left
💡 Hint
Think about horizontal distances from root in a perfect binary tree of height 3.
✗ Incorrect
In a perfect binary tree of height h, horizontal distances range from -h to +h, so total columns = 2*h + 1.
