Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the queue with the root node and its horizontal distance 0.
DSA Python
queue = [(root, [1])]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.
✗ Incorrect
The root node is at horizontal distance 0, so we start the queue with (root, 0).
2fill in blank
mediumComplete 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]))
Attempts:
3 left
💡 Hint
Common Mistakes
Adding 1 instead of subtracting 1 for left child.
Using the same hd for left child.
✗ Incorrect
The left child is one step to the left, so its horizontal distance is current hd minus 1.
3fill in blank
hardFix 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]))
Attempts:
3 left
💡 Hint
Common Mistakes
Subtracting 1 instead of adding 1 for right child.
Using the same hd for right child.
✗ Incorrect
The right child is one step to the right, so its horizontal distance is current hd plus 1.
4fill in blank
hardFill 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)
Attempts:
3 left
💡 Hint
Common Mistakes
Using node or node.val as dictionary keys instead of hd.
Using the dictionary name as a key.
✗ Incorrect
We check if the horizontal distance hd is in the dictionary and initialize a list if not.
5fill in blank
hardFill all three blanks to return the vertical order traversal sorted by horizontal distance.
DSA Python
return [column_table[[1]] for [2] in sorted([3])]
Attempts:
3 left
💡 Hint
Common Mistakes
Using hd or node.val instead of a loop variable for keys.
Not sorting the keys before returning.
✗ Incorrect
We sort the keys of the dictionary and collect the lists in order to return the vertical order traversal.
