Bird
Raised Fist0

Given the following memoization cache after computing max gains for nodes in a binary tree: {NodeA: 5, NodeB: 3, NodeC: 0} If NodeA is the root with left child NodeB and right child NodeC, and max_sum is 10, which of the following could be the node values for NodeA, NodeB, and NodeC?

hard🔄 Reverse Engineer Q9 of Q15
Tree: Depth-First Search - Binary Tree Maximum Path Sum
Given the following memoization cache after computing max gains for nodes in a binary tree: {NodeA: 5, NodeB: 3, NodeC: 0} If NodeA is the root with left child NodeB and right child NodeC, and max_sum is 10, which of the following could be the node values for NodeA, NodeB, and NodeC?
ANodeA=2, NodeB=3, NodeC=0
BNodeA=4, NodeB=3, NodeC=1
CNodeA=5, NodeB=3, NodeC=-2
DNodeA=1, NodeB=5, NodeC=4
Step-by-Step Solution
Solution:
  1. Step 1: Understand memo values

    Memo stores max gain from node to parent, ignoring negative gains (0 if negative).
  2. Step 2: Check consistency with max_sum=10

    max_sum = NodeA.val + left_gain + right_gain = 5 + 3 + 2 (since NodeC gain is 0, NodeC.val must be negative to yield 0 gain). NodeA=5, NodeB=3, NodeC=-2 fits this with NodeC=-2.
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    Memo gains and max_sum consistent only in NodeA=5, NodeB=3, NodeC=-2 [OK]
Quick Trick: Memo stores max(0, subtree gain), negative values yield 0 gain [OK]
Common Mistakes:
MISTAKES
  • Assuming memo stores raw node values
  • Ignoring zero gain for negative subtree
  • Confusing max_sum with memo values
Trap Explanation:
PITFALL
  • Candidates often misinterpret memo gains as node values or ignore zeroing negative gains.
Interviewer Note:
CONTEXT
  • Tests deep understanding of memoization state and max sum calculation.
Master "Binary Tree Maximum 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