House Robber III (LeetCode 337): Solution and Explanation
Houses form a binary tree and each holds some money. You may not rob a house and its direct child on the same night. Return the most money you can take.
Intuition
A first guess is to rob every other level of the tree. It fails as soon as one branch is rich at depth 2 while another is rich at depth 1, because levels force the same choice on every branch.
The rule only links a node to its parent and its children. So once you know, for a child's subtree, the best loot when the child is robbed and the best loot when it is not, you no longer care what happens deeper down.
That gives each node two numbers. If you rob it, both children must be left alone, so you add their 'not robbed' totals. If you leave it, each child is free and takes whichever of its two totals is larger.
Approach: Postorder DFS returning (take, skip)
- Define
dfs(node)that returns a pair(take, skip)for the subtree rooted atnode. An empty subtree returns(0, 0). - Call
dfson the left and right children first. take = node.val + left.skip + right.skip: robbing the node forbids robbing its children.skip = max(left.take, left.skip) + max(right.take, right.skip): leaving the node lets each child choose its best option.- Return
max(take, skip)for the root.
Step-by-step example
root = [3,2,3,null,3,null,1]
- The leaf 3 (right child of 2) gets take = 3, skip = 0. The leaf 1 gets take = 1, skip = 0.
- House 2 has only the right child 3. take = 2 + 0 = 2 and skip = max(3, 0) = 3.
- House 3 on the right has only the child 1. take = 3 + 0 = 3 and skip = max(1, 0) = 1.
- The root 3: take = 3 + skip(2) + skip(3) = 3 + 3 + 1 = 7, and skip = max(2, 3) + max(3, 1) = 6.
- Return max(7, 6) = 7: rob the root and the two grandchildren 3 and 1.
Code: Python, Java, C++
1class Solution:
2 def rob(self, root: Optional[TreeNode]) -> int:
3 def dfs(node):
4 if not node:
5 return (0, 0)
6 left = dfs(node.left)
7 right = dfs(node.right)
8 take = node.val + left[1] + right[1]
9 skip = max(left) + max(right)
10 return (take, skip)
11
12 return max(dfs(root))1class Solution {
2 public int rob(TreeNode root) {
3 int[] res = dfs(root);
4 return Math.max(res[0], res[1]);
5 }
6
7 private int[] dfs(TreeNode node) {
8 if (node == null) return new int[]{0, 0};
9 int[] left = dfs(node.left);
10 int[] right = dfs(node.right);
11 int take = node.val + left[1] + right[1];
12 int skip = Math.max(left[0], left[1]) + Math.max(right[0], right[1]);
13 return new int[]{take, skip};
14 }
15}1class Solution {
2 pair<int, int> dfs(TreeNode* node) {
3 if (!node) return {0, 0};
4 pair<int, int> l = dfs(node->left);
5 pair<int, int> r = dfs(node->right);
6 int take = node->val + l.second + r.second;
7 int skip = max(l.first, l.second) + max(r.first, r.second);
8 return {take, skip};
9 }
10
11public:
12 int rob(TreeNode* root) {
13 pair<int, int> res = dfs(root);
14 return max(res.first, res.second);
15 }
16};Time and space complexity
O(n)
Each node is visited once and does a constant amount of work combining its children's pairs.
O(h)
Only the recursion stack is used, and its depth is the height h of the tree: O(log n) when balanced, O(n) for a chain.
Other approaches
Recursion with grandchildren and a memo (time O(n), space O(n))
rob(node) = max(node.val + rob of all four grandchildren, rob(left) + rob(right)), cached per node in a hash map. It is correct and linear with the memo, but it needs the extra map and reads more nodes per call.
Plain recursion without memo (time O(2^h) roughly, space O(h))
The same recurrence without caching recomputes subtrees again and again, which blows up to exponential time on deep trees.
Common mistakes
- Robbing alternate levels. Different branches may want different parity, so a level-based answer can be far from optimal.
- Using
left.take + right.takefor skip. A free child should take the larger of its two values, and sometimes skipping it is better. - Writing the recursion without memoization or the pair trick, which times out on large trees.
- Forgetting that values can be 0, so ties between take and skip are common; either choice gives the same total.
Frequently asked questions
Why are two numbers per node enough?
The only constraint involving a node is with its parent and children. If the parent knows the best total of a child's subtree for both cases, robbed and not robbed, it has everything it needs to decide.
How is this related to House Robber I?
House Robber I keeps the same two states, robbed or not, along a line of houses. Here the line becomes a tree, and the states flow from children to parent instead of from left to right.
Can I recover which houses to rob?
Yes. Start at the root with the larger of its two values. A robbed node forces its children to skip; a skipped node lets each child pick its larger value. Walking down this way marks the houses.