Bird
Raised Fist0

Given the binary tree: 1 / \ 2 3 What is the final value of the variable total returned by sumNumbers?

easy🧾 Code Trace Q12 of Q15
Tree: Depth-First Search - Sum Root to Leaf Numbers
Consider the following Python code implementing the Morris Preorder Traversal approach to sum root-to-leaf numbers. Given the binary tree: 1 / \ 2 3 What is the final value of the variable total returned by sumNumbers?
A5
B15
C25
D26
Step-by-Step Solution
  1. Step 1: Trace path 1->2

    current_number accumulates 1 then 12; leaf node 2 adds 12 to total.
  2. 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.
  3. Step 3: Check for off-by-one or missed increments

    Integer division after visiting left subtree correctly adjusts current_number; no extra addition occurs.
  4. Final Answer:

    Option C -> Option C
  5. Quick Check:

    Sum of 12 and 13 is 25, matching code behavior [OK]
Quick Trick: Trace current_number updates carefully at each node [OK]
Common Mistakes:
MISTAKES
  • Off-by-one in current_number division
  • Adding non-leaf nodes to total
Trap Explanation:
PITFALL
  • Integer division of current_number after visiting left subtree can cause subtle off-by-one errors if mishandled.
Interviewer Note:
CONTEXT
  • Tests candidate's ability to mentally execute Morris traversal and track variable state.
Master "Sum Root to Leaf Numbers" in Tree: Depth-First Search

3 interactive learning modes - each teaches the same concept differently

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Tree: Depth-First Search Quizzes