Practice
inorderTraversal on this tree?Tree structure:
2
/ \ 1 3
from typing import Optional, List
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
def inorderTraversal(root: Optional[TreeNode]) -> List[int]:
result = []
current = root
while current:
if not current.left:
result.append(current.val) # visit node
current = current.right
else:
predecessor = current.left
while predecessor.right and predecessor.right != current:
predecessor = predecessor.right
if not predecessor.right:
predecessor.right = current # create thread
current = current.left
else:
predecessor.right = None # remove thread
result.append(current.val) # visit node
current = current.right
return result
Solution
Step 1: Trace traversal starting at root=2
Current=2 has left child 1, find predecessor in left subtree: node 1 (no right child). Create thread from 1.right to 2, move current to 1.Step 2: Visit node 1 (no left child), append 1, move current to 1.right which points to 2 (thread).
Remove thread, append 2, move current to 2.right=3. Node 3 has no left child, append 3, move current to null.Final Answer:
Option A -> Option AQuick Check:
Inorder traversal of tree 1,2,3 is [1,2,3] [OK]
- Appending before removing thread
- Visiting root before left subtree
- Confusing preorder with inorder output
total returned by sumNumbers?Solution
Step 1: Trace path 1->2
current_number accumulates 1 then 12; leaf node 2 adds 12 to total.Step 2: Trace path 1->3
current_number resets to 1, then accumulates 13; leaf node 3 adds 13 to total. Total = 12 + 13 = 25.Step 3: Check for off-by-one or missed increments
Integer division after visiting left subtree correctly adjusts current_number; no extra addition occurs.Final Answer:
Option C -> Option CQuick Check:
Sum of 12 and 13 is 25, matching code behavior [OK]
- Off-by-one in current_number division
- Adding non-leaf nodes to total
Solution
Step 1: Identify inorder_index initialization
inorder_index should start at the last index (len(inorder) - 1) because postorder is processed from end to start.Step 2: Consequences of wrong initialization
Starting at 0 causes incorrect comparisons and popping logic, leading to wrong tree structure or runtime errors.Final Answer:
Option B -> Option BQuick Check:
Correct inorder_index initialization is critical for stack popping logic [OK]
- Wrong inorder_index initialization
- Confusing postorder traversal direction
- Incorrect stack popping conditions
Solution
Step 1: Understand recursion stack limitations
Deep trees can cause recursion stack overflow in recursive solutions.Step 2: Identify iterative approach benefits
Iterative postorder traversal uses explicit stack, avoiding recursion limits and still computes heights and balance in O(n) time.Final Answer:
Option B -> Option BQuick Check:
Iterative approach avoids recursion depth issues [OK]
- Assuming recursion with tail call optimization works in Python
- Using BFS which does not check subtree height balance
- Choosing brute force recursion despite stack limits
Solution
Step 1: Understand effect of negative values
Negative values can decrease current_sum, so pruning based on exceeding targetSum can miss valid paths.Step 2: Adjust DFS to remove pruning
To ensure all valid paths are found, pruning must be removed and all paths fully explored.Final Answer:
Option A -> Option AQuick Check:
Pruning breaks correctness with negative values; full exploration needed [OK]
- Applying pruning blindly
- Switching to BFS unnecessarily
