Tree DP: Answers That Flow Up

Data Structures & Algorithms · Advanced Dynamic Programming · Advanced · 25 min

Solve a tree problem by letting every node combine its children's answers in post-order: return a pair when one number is not enough (house robber III), keep the returned value apart from the recorded answer (max path sum), and reroot to answer for every node.

This lesson is part of TuringPath Pro. See plans.

What you will learn

  • Children first: post-order is the fill order
  • Return a pair, not a number
  • What you return is not always the answer
  • Rerooting: an answer for every root in O(n)

More in Advanced Dynamic Programming

Previous: Interval DP: Ranges, Splits and the Last Move · Next: Digit DP: Counting Numbers Up to N