DSA problem
House Robber III
The thief has discovered a new place to rob — a binary tree of houses. Each node holds a non-negative integer amount of money. The root is the entrance, and every other house has exactly one parent house. The catch is the same alarm rule as in the array version: two directly-linked houses (a parent and its child) cannot both be robbed on the same night. Given the root of the binary tree, return the maximum amount of money the thief can steal without triggering the alarm.
A naive recursion considers two choices at every node: rob it (so neither child can be robbed) or skip it (each child is then free to make its own optimal choice). The recurrence rob(node) = max(node.val + skip(left) + skip(right), rob(left) + rob(right)) is correct, but if implemented as two separate recursive calls per subtree it explodes into exponential overlapping work — each call to rob triggers calls to skip on its children, and each call to skip recursively invokes rob and skip again on the same subtrees.
Memoization on the node reference fixes the blowup, but the cleanest fix is structural: collapse both pieces of information into a single post-order traversal. For every subtree rooted at node, compute a pair (rob, notRob), where rob is the best total assuming we DO rob node and notRob is the best total assuming we DO NOT. The pair from a single recursive call delivers everything its parent needs.
The recurrences are then trivial: rob = node.val + leftNotRob + rightNotRob (children must be skipped), and notRob = max(leftRob, leftNotRob) + max(rightRob, rightNotRob) (each child independently picks its best option). The final answer at the root is max(rob, notRob). This runs in O(n) time visiting each node once, and O(h) space for the recursion stack where h is the height of the tree.
Examples
root = [3,2,3,null,3,null,1]7root = [3,4,5,1,3,null,1]9root = [2,1,3,null,4]7Constraints
- The number of nodes in the tree is in the range [1, 10^4].
- 0 <= Node.val <= 10^4
- The given input is a valid binary tree.
3 hints and a full solution walkthrough with step-by-step visualizations are available in the practice editor.