Practice
max_sum after the function completes?
Tree structure:
```
1
/
2 3
```
class TreeNode:
def __init__(self, val=0, left=null, right=null):
self.val = val
self.left = left
self.right = right
def maxPathSum(root):
if not root:
return 0
max_sum = float('-inf')
stack = []
node = root
last_visited = null
gain_map = {}
while stack or node:
if node:
stack.append(node)
node = node.left
else:
peek = stack[-1]
if peek.right and last_visited != peek.right:
node = peek.right
else:
left_gain = max(gain_map.get(peek.left, 0), 0)
right_gain = max(gain_map.get(peek.right, 0), 0)
current_path_sum = peek.val + left_gain + right_gain
max_sum = max(max_sum, current_path_sum)
gain_map[peek] = peek.val + max(left_gain, right_gain)
last_visited = stack.pop()
return max_sum
Solution
Step 1: Trace left subtree gain
Node 2 has no children, so left_gain=0, right_gain=0, current_path_sum=2, max_sum updated to 2, gain_map[2]=2.Step 2: Trace root and right subtree
Node 3 has no children, current_path_sum=3, max_sum updated to 3, gain_map[3]=3. For root 1, left_gain=2, right_gain=3, current_path_sum=1+2+3=6, max_sum updated to 6.Final Answer:
Option C -> Option CQuick Check:
Sum of path 2 -> 1 -> 3 is 6 [OK]
- Ignoring one child's gain leads to 5 or 4
- Off-by-one in gain_map updates
- Returning partial sums instead of max path sum
def hasPathSum(root: Optional[TreeNode], targetSum: int) -> bool:
def dfs(node, curr_sum):
if not node:
return False
curr_sum += node.val
if not node.left and not node.right:
return curr_sum == targetSum
return dfs(node.left, curr_sum) or dfs(node.right, curr_sum)
return dfs(root, 0)
Solution
Step 1: Trace the path sums
Check root-to-leaf paths: - 5↔4↔11↔7 = 27 - 5↔4↔11↔2 = 22 (matches targetSum) - 5↔8↔13 = 26 - 5↔8↔4↔1 = 18 Since one path sums to 22, the function returns True.Step 2: Confirm code logic
The DFS returns True as soon as it finds the matching path sum at a leaf node.Final Answer:
Option A -> Option AQuick Check:
Path 5↔4↔11↔2 sums to 22, so return True [OK]
- Stopping at non-leaf nodes
- Ignoring leaf node condition
Solution
Step 1: Analyze height computation calls
Height is computed recursively for each node, and each height call traverses subtree nodes.Step 2: Calculate total calls
For n nodes, height is called at each node, and each call can take O(n) in worst case, leading to O(n^2) total time.Final Answer:
Option A -> Option AQuick Check:
Repeated height calls cause quadratic time [OK]
- Assuming height calls are O(1) leading to O(n) time
- Confusing height with depth or tree height h
- Thinking O(n log n) due to balanced tree assumption
def exists(idx, h, root):
left, right = 0, 2**(h - 1) - 1
for _ in range(h - 1):
mid = (left + right) // 2
if idx < mid: # Bug here
root = root.left
right = mid
else:
root = root.right
left = mid + 1
return root is not null
Solution
Step 1: Understand binary search condition
The condition should be 'if idx <= mid' to correctly include mid index in left subtree.Step 2: Identify impact of bug
Using 'idx < mid' excludes mid index from left subtree, causing incorrect traversal and wrong node existence checks.Final Answer:
Option A -> Option AQuick Check:
Correct binary search must include mid index [OK]
- Using < instead of <= in binary search condition
Solution
Step 1: Understand the problem extension
Nodes now have arbitrary children, so left/right pointers are insufficient.Step 2: Modify BFS to handle multiple children
Iterate over 'node.children' list and enqueue each child to explore all branches correctly.Step 3: Evaluate other options
Ignoring children misses nodes; DFS can work but BFS is still valid; summing depths is incorrect for max depth.Final Answer:
Option A -> Option AQuick Check:
Enqueue all children ensures full traversal for max depth [OK]
- Ignoring extra children
- Assuming binary tree structure
- Summing depths instead of max
