Practice
Solution
Step 1: Understand the problem constraints
The problem requires checking balance at every node efficiently without recomputing heights multiple times.Step 2: Identify the approach that avoids redundant work
Postorder traversal that returns both height and balance status in one pass allows early exit on imbalance and avoids repeated height calculations.Final Answer:
Option D -> Option DQuick Check:
Postorder traversal with early exit is O(n) and avoids recomputation [OK]
- Checking balance only at root misses subtree imbalances
- Computing height separately for each node causes O(n²) time
- Using BFS to check balance is incorrect as balance depends on subtree heights
inorder_index after processing the first iteration of the for-loop when inorder = [9,3,15,20,7] and postorder = [9,15,7,20,3]?Solution
Step 1: Initialize variables
inorder_index starts at 4 (last index of inorder). The root is 3 (postorder[-1]).Step 2: First iteration (i=3, node_val=20)
top.val=3 != inorder[4]=7, so attach 20 as right child, stack grows, inorder_index stays 4.Step 3: Second iteration (i=2, node_val=7)
top.val=20 != inorder[4]=7 is false, so enter else: pop stack while top.val == inorder[inorder_index]. 20 != 7, so no pop, attach 7 as right child, stack grows, inorder_index stays 4.Step 4: Third iteration (i=1, node_val=15)
top.val=7 == inorder[4]=7, pop 7, inorder_index=3; top.val=20 == inorder[3]=20, pop 20, inorder_index=2; top.val=3 != inorder[2]=15, stop popping. Attach 15 as left child, stack grows.Final Answer:
Option D -> Option DQuick Check:
inorder_index decremented twice after popping 7 and 20 [OK]
- Off-by-one error in inorder_index update
- Confusing when to pop stack
- Misreading top.val vs inorder[inorder_index]
Solution
Step 1: Understand the problem requires the longest path between any two nodes, not necessarily passing through root.
The diameter can be found by checking the sum of left and right subtree heights at every node.Step 2: Recognize that a postorder traversal with recursion and a global variable to track diameter updates at each node ensures all paths are considered.
This approach visits each node once, updating the diameter with left_height + right_height.Final Answer:
Option A -> Option AQuick Check:
Postorder traversal with global diameter update covers all nodes [OK]
- Thinking diameter must pass through root
- Using greedy to pick deeper subtree only
- Confusing diameter with twice height
def inorderTraversal(root):
result = []
current = root
while current:
if not current.left:
result.append(current.val)
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:
# BUG: missing line to remove thread
result.append(current.val)
current = current.right
return result
Solution
Step 1: Identify thread removal step
Morris traversal requires removing the temporary thread by setting predecessor.right = None after visiting the node.Step 2: Locate missing removal
The code lacks the line resetting predecessor.right = None, causing infinite loops or corrupted tree structure.Final Answer:
Option A -> Option AQuick Check:
Without removing thread, traversal loops endlessly [OK]
- Forgetting to remove thread
- Appending node before removing thread
- Incorrectly creating thread
Solution
Step 1: Understand path direction relaxation
Allowing upward and downward moves means paths are any connected sequences, not just parent-to-child.Step 2: Model tree as undirected graph and run DFS from every node
To count all paths, treat tree as undirected graph and run DFS from each node, using prefix sums and resetting counts to avoid cross-branch contamination.Final Answer:
Option A -> Option AQuick Check:
Undirected DFS from every node with prefix sums counts all connected paths [OK]
- Trying to reuse one-directional prefix sum DFS without modification
- Assuming running DFS twice covers all paths
- Trying to store both directions in one prefix sum map
