Unit 6 of 9 · about 3 hours · derive, implement
Tree dynamic programming
On trees the dependency graph is the input. One post-order pass gives linear time; the only design work is the per-node state.
You'll be able to
- Derive per-node state that carries every choice the parent must reason about.
- Implement post-order helpers that return tuples or split return-vs-global quantities.
- Handle skewed trees within the recursion limit or with an explicit stack.
Covers
Read
In this order, only the parts listed
0 of 3 done
- Maximum independent set on treesAlgorithms - dynamic programming
Section 3.10 (pp. 120-122): recurrence over children and grandchildren, memoization into the tree, post-order evaluation, and the mutual recurrence
- Tuple state and the return-vs-global splitDSA Handbook - dynamic programming
Two-state DP: the tuple on LC 337; The dual-quantity split, with negative values in play, on LC 124 with the clamp
- The linear precursorDSA Handbook - dynamic programming
House robber (LC 198): the take-or-skip prototype that LC 337 lifts onto trees, with the
[2,1,1,2]parity-greedy counterexample
Focus on
- Single-number returns fail House Robber III because the parent needs both worlds. Return and let the parent pick per scenario.
- LC 124 returns the straight-down chain but compares the bent path globally. Returning the bent path builds two-bend shapes that are not paths.
- Each node is visited once with local work, so time is . Space is the recursion stack , which is on skewed input.
Common mistakes · 3
- Keeping the default limit of 1000 for deep trees. LC 337 allows nodes and LC 124 allows , so a skewed tree raises
RecursionError; raise the limit once at module top level or use an explicit stack. - Initializing the LC 124 global to 0. All-negative trees then return 0 for an empty path; start at negative infinity so the root comparison fires.
- Recomputing grandchildren per node instead of returning both states. The tuple memoizes both worlds in one pass; without it the recursion revisits subtrees.
Try it yourself
Closed-book, before checking anything
rob on level-order [3,2,3,null,3,null,1] = 7 and on [3,4,5,1,3,null,1] = 9. State both level-order inputs and both expected outputs.Quiz yourself
Answer first, then mark it. Missed ones go to Review.
0 got · 0 missed
1.Write the House Robber III recurrence.Answer
; of each child, summed. Answer at the root.
2.Why is the empty-subtree identity ?Answer
An empty subtree contributes no income and imposes no obligation, so both slots add zero to the parent sums.
3.LC 124: which quantity travels up and why?Answer
The straight-down chain . The parent can only extend a linear chain; a bent path extended further would bend twice.
4.How does relate to the tuple?Answer
It is the same pattern: is , and is .
Practice
- LeetCode 337 - House Robber III
Attempt cold. Check
[3,2,3,null,3,null,1] = 7and[3,4,5,1,3,null,1] = 9, then submit.Attempt cold: return the tuple from the helper. After submitting, read the AlgoMaster House Robber III lesson (https://algomaster.io/learn/dsa/house-robber-iii).
You're done when
Both reproduce trees return 7 and 9 from a blank file, and you can state what each helper slot means to its parent.
Only when you can do this without notes.
Leads to
Notes
Resources · 2 for this unit, 6 more in the section
- DSA Handbook - dynamic programmingHandbook Academy · primary
Study Part 9 in order from state and recurrence through sequence, grid, tree and bitmask families. For every example, close the page and reconstruct the state, transition and evaluation order.
- Algorithms - dynamic programmingJeff Erickson · required
Use the dynamic-programming chapter to derive states and recurrences from first principles and to separate DP from greedy reasoning and shortest paths.
Also in this section
- AlgoMaster DSA - dynamic programming sliceAlgoMaster · required
Attempt Coin Change, Longest Common Subsequence and House Robber III cold. Write the state, transition, base case and evaluation order first; compare against brute force on small inputs and reconstruct a witness when available.
- Hypothesis - exhaustive and differential DP testsHypothesis · support
Generate small valid instances, compare memoized, tabulated and exhaustive results, and retain shrunk failures for wrong state, base case, order and compression defects.
- NeetCode - unseen mixed problemsNeetCode · optional
Use two tag-hidden mixed problems as the final transfer test. One must require rejecting a tempting greedy or insufficient-state solution with a counterexample.
- Python sys: recursion limitPython Software Foundation · reference
Read
sys.getrecursionlimitandsys.setrecursionlimit: the limit guards the C stack and its safe maximum is platform-dependent. Check the default withgetrecursionlimit(1000 on CPython 3.13), raise it once for memoized DP, or switch to tabulation for deep inputs. - Erickson lecture note D - Advanced Dynamic ProgrammingJeff Erickson · reference
Read Section D.1 (Saving Space: Divide and Conquer) for the Hirschberg Half-table split that recovers an optimal edit sequence in space.
- Erickson lecture note J - Approximation AlgorithmsJeff Erickson · reference
Read Section J.1 (Load Balancing) for the Partition reduction that makes exact scheduling NP-hard, the
GreedyLoadBalanceheuristic, and Theorem J.1 with its factor-2 proof.