Bird
Raised Fist0

Given the following optimal DFS code for Path Sum, what is the return value for the tree with root=5, left=4, right=8, left.left=11, targetSum=20?

easy🧾 Code Trace Q3 of Q15
Tree: Depth-First Search - Path Sum
Given the following optimal DFS code for Path Sum, what is the return value for the tree with root=5, left=4, right=8, left.left=11, targetSum=20?
AFalse
BRaises exception
CNone
DTrue
Step-by-Step Solution
Solution:
  1. Step 1: Trace dfs on left path 5->4->11

    Sum = 5 + 4 + 11 = 20, matches targetSum.
  2. Step 2: Return True at leaf node where sum equals target

    DFS returns True, so overall function returns True.
  3. Final Answer:

    Option D -> Option D
  4. Quick Check:

    Path sum found -> True [OK]
Quick Trick: Sum along root-to-leaf path equals target -> True [OK]
Common Mistakes:
MISTAKES
  • Stopping at non-leaf nodes
  • Miscomputing sum accumulation
Trap Explanation:
PITFALL
  • Candidates may forget to check leaf node condition and return prematurely.
Interviewer Note:
CONTEXT
  • Tests ability to mentally execute DFS with sum accumulation.
Master "Path Sum" 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