Tree DP
Tree DP computes answers for each subtree from its children’s answers, as in house robber on a tree and placing cameras.
Before you start: Bitmask DP: TSP and assignments, Recursive problems on trees: height, size, mirror, paths.
The idea
Houses form a tree. Robbing a house and one of its children sets off the alarm.
Intuition
Robbing the root and everything below it is not best. Here that plan takes 8.
Skipping the root and robbing its two children takes 9. The best choice at a house depends on its subtrees.
Whether to rob the root depends on what the children can do without it. So each subtree must report its answer in both cases.
Each house reports two numbers: the best total if it is robbed, and the best if it is skipped. The parent combines them.
Complexity
Each house is finished once from its two children: O(n) time. Trying every set of houses costs 2^n.
The recursion is as deep as the tree is tall: O(h) space. A chain of n houses makes h equal to n.
Each open call holds one house's pair until its children return, so the stack grows with the longest root-to-leaf path.
Pitfalls
Example tree 1, 4, 4: a skipped house does not force its children to be skipped. It takes each child's better total, here 4 + 4.
Returning one number makes a parent ask its grandchildren again. Return the pair so each house is solved once.
Variations
Longest univalue path: each node reports its longest equal chain going down. The two chains at a node join into one path.
Binary tree cameras: each node reports needs cover, camera or covered. A node with an uncovered child gets a camera.
Code
Pseudocode
houseRobberIII(root):
best(node):
if node = null: return (0, 0)
(leftRob, leftSkip) ← best(node.left)
(rightRob, rightSkip) ← best(node.right)
robIt ← node.val + leftSkip + rightSkip
skipIt ← max(leftRob, leftSkip) + max(rightRob, rightSkip)
return (robIt, skipIt)
return max of best(root)
Python
def rob(root):
def best(node):
if node is None:
return 0, 0
left_rob, left_skip = best(node.left)
right_rob, right_skip = best(node.right)
rob_it = node.val + left_skip + right_skip
skip_it = max(left_rob, left_skip) + max(right_rob, right_skip)
return rob_it, skip_it
return max(best(root))
JavaScript
function rob(root) {
function best(node) {
if (node === null) return [0, 0];
const [leftRob, leftSkip] = best(node.left);
const [rightRob, rightSkip] = best(node.right);
const robIt = node.val + leftSkip + rightSkip;
const skipIt = Math.max(leftRob, leftSkip) + Math.max(rightRob, rightSkip);
return [robIt, skipIt];
}
return Math.max(...best(root));
}
Practice problems (5)
What does a house report? Warm-up
In house robber III, the walk goes from the leaves up to the root. Each house tells its parent two numbers. Which two?
Fill the two numbers Warm-up
Follow house robber on a four-house tree. Each house shows two numbers: the best total if it is robbed, then the best if it is skipped. Answer each question before the step plays.
House robber III Standard
The houses of a village form a binary tree. root is the first house, and every house holds some money.
The thief cannot rob two houses that are linked by an edge, a parent and one of its children, or the police are called. Return the most money that can be stolen.
Longest univalue path Standard
root is a binary tree. Find the longest path between any two nodes in which every node holds the same value. The path may or may not pass through the root.
Return its length counted in edges.
Binary tree cameras Standard
root is a binary tree. A camera placed on a node watches that node, its parent and its children.
Return the fewest cameras that watch every node.
Next on the path: Recognizing DP and choosing the state