Bird
Raised Fist0
DSA Pythonprogramming~10 mins

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

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

Complete the code to initialize the queue with the root node and its horizontal distance 0.

DSA Python
queue = [(root, [1])]
Drag options to blanks, or click blank then click option'
A1
B0
C-1
DNone
Attempts:
3 left
💡 Hint
Common Mistakes
Starting horizontal distance at 1 or -1 instead of 0.
Using None instead of a number for horizontal distance.
2fill in blank
medium

Complete the code to add the left child to the queue with horizontal distance one less than current.

DSA Python
if node.left:
    queue.append((node.left, [1]))
Drag options to blanks, or click blank then click option'
Ahd - 1
Bhd + 1
Chd
Dhd * 2
Attempts:
3 left
💡 Hint
Common Mistakes
Adding 1 instead of subtracting 1 for left child.
Using the same hd for left child.
3fill in blank
hard

Fix the error in the code to add the right child to the queue with correct horizontal distance.

DSA Python
if node.right:
    queue.append((node.right, [1]))
Drag options to blanks, or click blank then click option'
Ahd + 1
Bhd
Chd - 1
Dhd * 2
Attempts:
3 left
💡 Hint
Common Mistakes
Subtracting 1 instead of adding 1 for right child.
Using the same hd for right child.
4fill in blank
hard

Fill both blanks to correctly update the dictionary with node values grouped by horizontal distance.

DSA Python
if [1] not in column_table:
    column_table[[2]] = []
column_table[hd].append(node.val)
Drag options to blanks, or click blank then click option'
Ahd
Bnode
Cnode.val
Dcolumn_table
Attempts:
3 left
💡 Hint
Common Mistakes
Using node or node.val as dictionary keys instead of hd.
Using the dictionary name as a key.
5fill in blank
hard

Fill all three blanks to return the vertical order traversal sorted by horizontal distance.

DSA Python
return [column_table[[1]] for [2] in sorted([3])]
Drag options to blanks, or click blank then click option'
Ahd
Bkey
Ccolumn_table.keys()
Dnode.val
Attempts:
3 left
💡 Hint
Common Mistakes
Using hd or node.val instead of a loop variable for keys.
Not sorting the keys before returning.

Practice

(1/5)
1. What does Vertical Order Traversal of a binary tree mean?
easy
A. Nodes are grouped by their horizontal rows from top to bottom
B. Nodes are visited level by level from top to bottom
C. Nodes are grouped by their vertical columns from left to right
D. Nodes are visited in preorder (root, left, right)

Solution

  1. Step 1: Understand vertical columns in a tree

    Vertical order groups nodes by their horizontal distance from the root, called columns.
  2. Step 2: Define vertical order traversal

    It means collecting nodes column-wise from leftmost to rightmost column.
  3. Final Answer:

    Nodes are grouped by their vertical columns from left to right -> Option C
  4. Quick Check:

    Vertical order = group nodes by columns [OK]
Hint: Think columns from left to right for vertical order [OK]
Common Mistakes:
  • Confusing vertical order with level order traversal
  • Thinking vertical order is preorder or inorder traversal
  • Mixing rows with columns in traversal
2. Which Python data structure is best to store nodes grouped by their vertical columns during vertical order traversal?
easy
A. Dictionary with column as key and list of nodes as value
B. List
C. Set
D. Tuple

Solution

  1. Step 1: Identify need to group nodes by column

    We need to map each column number to the nodes in that column.
  2. Step 2: Choose data structure for mapping

    A dictionary with column keys and list values is ideal for grouping nodes by column.
  3. Final Answer:

    Dictionary with column as key and list of nodes as value -> Option A
  4. Quick Check:

    Use dict for column to nodes mapping [OK]
Hint: Use dict to map columns to node lists [OK]
Common Mistakes:
  • Using list only without mapping columns
  • Using set which loses order
  • Using tuple which is immutable
3. What is the output of vertical order traversal for this tree?
    1
   / \
  2   3
 / \   \
4   5   6
medium
A. [[4, 2], [1, 5, 3], [6]]
B. [[4], [2], [1, 5], [3], [6]]
C. [[1], [2, 3], [4, 5, 6]]
D. [[6], [3], [1, 5], [2], [4]]

Solution

  1. Step 1: Assign column indices to nodes

    Root 1 is column 0; left child 2 is -1; right child 3 is +1; 4 is -2; 5 is 0; 6 is +2.
  2. Step 2: Group nodes by columns and order top to bottom

    Columns: -2:[4], -1:[2], 0:[1,5], 1:[3], 2:[6].
  3. Final Answer:

    [[4], [2], [1, 5], [3], [6]] -> Option B
  4. Quick Check:

    Group nodes by column indices [OK]
Hint: Assign columns: left -1, right +1 from root [OK]
Common Mistakes:
  • Mixing node order within same column
  • Ignoring column indices for children
  • Grouping nodes by rows instead of columns
4. Identify the bug in this vertical order traversal code snippet:
def vertical_order(root):
    if not root:
        return []
    col_table = {}
    queue = [(root, 0)]
    while queue:
        node, col = queue.pop()
        if col not in col_table:
            col_table[col] = []
        col_table[col].append(node.val)
        if node.left:
            queue.append((node.left, col - 1))
        if node.right:
            queue.append((node.right, col + 1))
    return [col_table[x] for x in sorted(col_table)]
medium
A. Using pop() removes last element, should use pop(0) for BFS
B. Dictionary keys should be strings, not integers
C. Appending node values instead of nodes causes error
D. Missing check for empty tree at start

Solution

  1. Step 1: Analyze queue usage for BFS

    Queue should remove elements from front (pop(0)) for BFS, but pop() removes from end (LIFO).
  2. Step 2: Identify impact on traversal order

    Using pop() causes DFS-like order, breaking vertical order traversal logic.
  3. Final Answer:

    Using pop() removes last element, should use pop(0) for BFS -> Option A
  4. Quick Check:

    Use pop(0) for BFS queue [OK]
Hint: Use pop(0) to dequeue for BFS, not pop() [OK]
Common Mistakes:
  • Using pop() instead of pop(0) in BFS queue
  • Confusing BFS with DFS traversal order
  • Not sorting dictionary keys before output
5. Given a binary tree where multiple nodes share the same vertical and horizontal distance, how should vertical order traversal output be ordered?
hard
A. Sort nodes by column only, ignoring row and value
B. Sort nodes by value only, ignoring position
C. Output nodes in insertion order without sorting
D. Sort nodes by their row (top to bottom), then by value ascending if tie

Solution

  1. Step 1: Understand tie cases in vertical order

    When nodes share same column and row, order by node value ascending to maintain consistency.
  2. Step 2: Define final ordering rule

    Sort nodes first by row (top to bottom), then by value ascending if rows tie.
  3. Final Answer:

    Sort nodes by their row (top to bottom), then by value ascending if tie -> Option D
  4. Quick Check:

    Row then value sorting for ties [OK]
Hint: Sort by row, then value for nodes sharing position [OK]
Common Mistakes:
  • Ignoring row order and sorting only by column
  • Not sorting nodes with same position by value
  • Outputting nodes in random or insertion order
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard