Practice
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
def levelOrderBottom(root):
levels = []
def dfs(node, level):
if not node:
return
if len(levels) == level:
levels.append([])
levels[level].append(node.val)
dfs(node.left, level + 1)
dfs(node.right, level + 1)
dfs(root, 0)
return levels[::-1]
root = TreeNode(3)
root.left = TreeNode(9)
root.right = TreeNode(20, TreeNode(15), TreeNode(7))
print(levelOrderBottom(root))Solution
Step 1: Trace dfs calls and levels
Level 0: [3], Level 1: [9, 20], Level 2: [15, 7]Step 2: Reverse levels list before returning
Reversing [[3], [9, 20], [15, 7]] yields [[15, 7], [9, 20], [3]]Final Answer:
Option A -> Option AQuick Check:
Output matches bottom-up level order traversal [OK]
- Forgetting to reverse levels
- Mixing up left and right children order
Solution
Step 1: Understand the problem constraints
The width includes null nodes between end-nodes, so counting only non-null nodes per level underestimates width.Step 2: Identify the approach that tracks positions
BFS with indexing assigns position indices to nodes, allowing calculation of width as the difference between first and last indices at each level. Normalizing indices prevents integer overflow on deep trees.Final Answer:
Option A -> Option AQuick Check:
Indexing with normalization handles sparse trees and deep levels correctly [OK]
- Counting only non-null nodes per level
- Using DFS without proper indexing
- Ignoring index normalization causing overflow
Solution
Step 1: Understand the problem requirement
The problem requires checking if a tree is symmetric, which means the left subtree is a mirror reflection of the right subtree.Step 2: Identify the approach that compares mirrored nodes efficiently
Using an iterative BFS with a queue to compare pairs of nodes from left and right subtrees simultaneously allows early detection of asymmetry and avoids unnecessary traversal.Final Answer:
Option D -> Option DQuick Check:
Iterative BFS with paired nodes and early termination is optimal for symmetry check [OK]
- Comparing left subtree to left subtree instead of mirrored right subtree
- Using greedy checks that miss deeper asymmetry
- Assuming symmetry can be checked by root and immediate children only
Solution
Step 1: Compare the buggy line with the correct logic
The correct connection is head.right.next = head.next.left, but here it uses head.next.right, which is incorrect.Step 2: Understand the impact
This causes right child's next pointer to point to the wrong node, breaking the chain.Final Answer:
Option D -> Option DQuick Check:
Right child's next must connect to next node's left child [OK]
- Confusing next.left and next.right
- Skipping null checks
- Not updating leftmost pointer
Solution
Step 1: Understand problem extension
Nodes can appear multiple times if they share column and row but differ by parent, so row and parent info must be tracked.Step 2: Evaluate modifications
Tracking (node, column, row) and sorting by parent ID ensures correct ordering. Ignoring row or parent loses correctness. Visited set prevents duplicates but may skip valid repeats.Final Answer:
Option A -> Option AQuick Check:
Tracking row and parent allows correct ordering of duplicates [OK]
- Ignoring row or parent info leads to incorrect grouping
- Using visited set that skips valid repeated nodes
