Challenge - 5 Problems
Vertical Order Traversal 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 given binary tree?
DSA Python
class Node: def __init__(self, val): self.val = val self.left = None self.right = None def vertical_order(root): from collections import defaultdict, deque if not root: return [] column_table = defaultdict(list) queue = deque([(root, 0)]) while queue: node, column = queue.popleft() if node is not None: column_table[column].append(node.val) queue.append((node.left, column - 1)) queue.append((node.right, column + 1)) return [column_table[x] for x in sorted(column_table)] # Tree construction root = Node(1) root.left = Node(2) root.right = Node(3) root.left.left = Node(4) root.left.right = Node(5) root.right.left = Node(6) root.right.right = Node(7) print(vertical_order(root))
Attempts:
2 left
💡 Hint
Think about how nodes are grouped by their horizontal distance from the root.
✗ Incorrect
Nodes are grouped by their vertical columns. Column -2 has [4], -1 has [2], 0 has [1,5,6], 1 has [3], and 2 has [7].
❓ Predict Output
intermediateOutput for Vertical Order Traversal with Overlapping Nodes
What is the output of vertical order traversal for this binary tree with overlapping nodes in the same vertical column?
DSA Python
class Node: def __init__(self, val): self.val = val self.left = None self.right = None def vertical_order(root): from collections import defaultdict, deque if not root: return [] column_table = defaultdict(list) queue = deque([(root, 0)]) while queue: node, column = queue.popleft() if node is not None: column_table[column].append(node.val) queue.append((node.left, column - 1)) queue.append((node.right, column + 1)) return [column_table[x] for x in sorted(column_table)] # Tree construction root = Node(3) root.left = Node(9) root.right = Node(8) root.left.right = Node(4) root.right.left = Node(1) root.right.right = Node(7) print(vertical_order(root))
Attempts:
2 left
💡 Hint
Remember nodes are grouped by their vertical column and visited level by level.
✗ Incorrect
The vertical columns are: -1: [9], 0: [3,4,1], 1: [8], 2: [7].
🧠 Conceptual
advancedUnderstanding Vertical Order Traversal Data Structure
Which data structure is most suitable to store nodes during vertical order traversal to maintain the order of nodes at the same vertical and horizontal level?
Attempts:
2 left
💡 Hint
Think about the order nodes are visited in a level-wise traversal.
✗ Incorrect
A queue is used for breadth-first traversal to process nodes level by level, preserving the order of nodes at the same vertical and horizontal level.
❓ Predict Output
advancedOutput of Vertical Order Traversal with Negative and Positive Columns
What is the output of vertical order traversal for the following binary tree?
DSA Python
class Node: def __init__(self, val): self.val = val self.left = None self.right = None def vertical_order(root): from collections import defaultdict, deque if not root: return [] column_table = defaultdict(list) queue = deque([(root, 0)]) while queue: node, column = queue.popleft() if node is not None: column_table[column].append(node.val) queue.append((node.left, column - 1)) queue.append((node.right, column + 1)) return [column_table[x] for x in sorted(column_table)] # Tree construction root = Node(10) root.left = Node(20) root.right = Node(30) root.left.left = Node(40) root.left.right = Node(50) root.right.right = Node(60) print(vertical_order(root))
Attempts:
2 left
💡 Hint
Check how the horizontal distance changes with left and right children.
✗ Incorrect
Columns are: -2: [40], -1: [20], 0: [10, 50], 1: [30], 2: [60].
🔧 Debug
expertIdentify the Error in Vertical Order Traversal Implementation
What error will the following vertical order traversal code produce when run on a non-empty binary tree?
DSA Python
class Node: def __init__(self, val): self.val = val self.left = None self.right = None def vertical_order(root): from collections import defaultdict if not root: return [] column_table = defaultdict(list) queue = [(root, 0)] while queue: node, column = queue.pop() if node is not None: column_table[column].append(node.val) queue.append((node.left, column - 1)) queue.append((node.right, column + 1)) return [column_table[x] for x in sorted(column_table)]
Attempts:
2 left
💡 Hint
Consider how the queue is used and how nodes are popped.
✗ Incorrect
Using list as a stack (pop from end) causes depth-first traversal, adding nodes repeatedly without proper level order, leading to infinite loop.
