Practice
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: Understand the problem requires mirrored subtree comparison
The problem asks if the tree is symmetric, meaning the left subtree is a mirror reflection of the right subtree.Step 2: Identify the approach that compares mirrored nodes recursively with early exit
The recursive DFS approach compares left subtree nodes with right subtree nodes in mirrored positions and returns false immediately if any mismatch is found, ensuring efficiency.Final Answer:
Option A -> Option AQuick Check:
Mirrored recursive DFS matches problem requirements [OK]
- Assuming preorder traversal palindrome check works
- Using level-order traversal without mirrored comparison
- Trying brute force subtree permutations
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
Solution
Step 1: Identify missing operation
Inside the if block, after creating node.left, the new node is not pushed onto the stack.Step 2: Consequences of missing stack append
Without pushing, the algorithm loses track of the left subtree root, causing incorrect tree or infinite loops.Final Answer:
Option B -> Option BQuick Check:
Stack must track all nodes to correctly build tree [OK]
- Forgetting to push left child nodes
- Incorrectly incrementing inorder_index
- Misinitializing root or stack
Solution
Step 1: Identify worst-case traversal
In the worst case, the algorithm visits every node once to check all root-to-leaf paths.Step 2: Analyze recursion and early stopping
Early stopping can prune some paths, but worst case still requires full traversal, so time is O(n).Final Answer:
Option C -> Option CQuick Check:
Each node visited once -> O(n) time [OK]
- Confusing height with total nodes
- Assuming early stopping always reduces complexity
