Practice
rob_current when dfs is called on the root node (value 3)?Solution
Step 1: Trace dfs on left child (value 2)
dfs(2) calls dfs(null) and dfs(3). dfs(null) returns (0,0). dfs(3) returns (3,0) because it has no children. So rob_current=3+0+0=3, not_rob_current=max(0,0)+max(0,0)=0. dfs(3) returns (3,0). For node 2: rob_current=2+0+0=2, not_rob_current=max(0,3)+max(0,0)=3. dfs(2) returns (2,3).Step 2: Trace dfs on right child (value 3)
dfs(3) calls dfs(null) and dfs(1). dfs(1) returns (1,0). So rob_current=3+0+0=3, not_rob_current=max(0,0)+max(1,0)=1. dfs(3) returns (3,1).Step 3: Calculate rob_current at root (value 3)
rob_current = 3 + left[1] + right[1] = 3 + 3 + 1 = 7.Final Answer:
Option A -> Option AQuick Check:
Sum matches known example output 7 [OK]
- Mixing rob and not_rob values for children
- Off-by-one in recursion returns
Solution
Step 1: Understand problem requires all root-to-leaf paths
Since we must find all paths, not just one, exhaustive exploration is needed.Step 2: Identify DFS with path tracking and backtracking
DFS explores each path fully, tracking the current path and sum, backtracking to explore alternatives.Final Answer:
Option B -> Option BQuick Check:
DFS explores all paths, greedy or BFS do not guarantee all paths [OK]
- Thinking greedy or BFS finds all paths
- Confusing DP for path enumeration
Solution
Step 1: Analyze time complexity
Using a hash map avoids repeated linear searches, so each node is processed once -> O(n) time.Step 2: Analyze space complexity
Hash map stores n elements, recursion stack can be up to O(n) in skewed trees, so total auxiliary space is O(n).Final Answer:
Option A -> Option AQuick Check:
Time is O(n), space includes recursion stack and hash map [OK]
- Assuming slicing causes O(n^2) time
- Ignoring recursion stack space
- Confusing balanced tree depth with complexity
class Solution:
def sumNumbers(self, root: TreeNode) -> int:
self.total = 0
def dfs(node, current_number):
if not node:
return
current_number = current_number * 10 + node.val
self.total += current_number # Bug here
if not node.left and not node.right:
return
dfs(node.left, current_number)
dfs(node.right, current_number)
dfs(root, 0)
return self.total
Solution
Step 1: Understand when sums should be added
Sum should only include complete root-to-leaf numbers, so addition must happen only at leaf nodes.Step 2: Identify incorrect addition
Adding current_number at every node (including non-leaf) causes partial numbers to be summed, inflating total incorrectly.Final Answer:
Option B -> Option BQuick Check:
Sum only at leaves fixes the bug [OK]
- Adding partial path sums at non-leaf nodes
- Not resetting total between calls
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
