Practice
Solution
Step 1: Understand the problem requires swapping children after their subtrees are inverted
The inversion must be done bottom-up to ensure subtrees are correctly mirrored before swapping.Step 2: Identify the approach that recursively inverts subtrees before swapping children
Depth-first postorder traversal fits this pattern, ensuring correct inversion in O(n) time.Final Answer:
Option B -> Option BQuick Check:
Postorder recursion swaps children after subtree inversion [OK]
- Thinking greedy swaps without recursion suffice
- Confusing DP with tree inversion
- Assuming BFS alone guarantees correct inversion
Solution
Step 1: Understand rob_current calculation
rob_current should be node.val plus the not_rob values of left and right children, because robbing current node forbids robbing immediate children.Step 2: Identify the bug
Line 5 incorrectly adds left[0] and right[0] (rob values of children), violating adjacency constraint.Final Answer:
Option A -> Option AQuick Check:
Using rob values of children overcounts and breaks correctness [OK]
- Mixing rob and not_rob indices in tuple
- Forgetting adjacency constraints
Solution
Step 1: Identify time complexity
BFS visits each node exactly once, so time complexity is O(n).Step 2: Identify space complexity and common misconception
Queue can hold up to O(n) nodes at the widest level, so auxiliary space is O(n). The misconception is thinking nodes are processed multiple times, leading to O(n^2).Final Answer:
Option A -> Option AQuick Check:
Each node enqueued and dequeued once; max queue size O(n) [OK]
- Assuming multiple visits per node
- Confusing sorting with traversal
- Ignoring queue space usage
def isSymmetric(root: Optional[TreeNode]) -> bool:
def isMirror(t1: Optional[TreeNode], t2: Optional[TreeNode]) -> bool:
if not t1 or not t2:
return True
if t1.val != t2.val:
return False
return isMirror(t1.left, t2.right) and isMirror(t1.right, t2.left)
return isMirror(root.left, root.right) if root else True
Solution
Step 1: Analyze base case for null nodes
The line returns true if either t1 or t2 is null, but it should only return true if both are null.Step 2: Identify that returning true when only one node is null causes false positives
This causes asymmetric trees with missing nodes on one side to be incorrectly considered symmetric.Final Answer:
Option B -> Option BQuick Check:
Base case must check both nodes null equality, not just one [OK]
- Returning true if only one node is null
- Forgetting to check node values before recursion
- Mixing left and right subtree comparisons
Solution
Step 1: Understand the problem change
The tree is no longer complete, so properties used by optimal methods do not hold.Step 2: Identify correct counting method
Only a full traversal (DFS or BFS) guarantees counting all nodes correctly in O(n) time.Final Answer:
Option D -> Option DQuick Check:
Optimal methods rely on completeness, which is lost here [OK]
- Trying to apply binary search on last level
- Assuming height checks still reduce complexity
