Algorithms · Visualized

DP: House Robber III (Tree DP)

Dynamic Programming

Break problems into overlapping subproblems and store answers.

Houses are arranged as a binary tree, and a robber cannot take a parent and its immediate child on the same night because they are directly connected by a monitored street. Time: O(n) average, O(n), one visit per node worst; space: O(h) call stack for tree height h, O(n) only for a completely skewed tree.

Big-O: The most-asked interview category — 35 classics here.

House Robber III — Tree DP

Dynamic Programming

Houses are arranged as a binary tree, and a robber cannot take a parent and its immediate child on the same night because they are directly connected by a monitored street. A single post-order traversal solves it: each subtree reports two numbers — the best total if its root is robbed, and the best total if its root is skipped — and the parent combines the children's reports. There is no exponential re-exploration because every node is visited exactly once and its two answers are computed from its children's answers.

How House Robber III (Tree DP) Works

  1. Recurse into the left and right children first (post-order), so both children already know their rob/skip pair when the parent is evaluated.
  2. If this node is robbed, neither child may be robbed: rob = node.value + left.skip + right.skip.
  3. If this node is skipped, each child is free to pick whichever of its own states is larger: skip = max(left.rob, left.skip) + max(right.rob, right.skip).
  4. Return the pair (rob, skip) to the caller; the answer at the root is max(rob, skip).
  5. Handle an empty child by returning (0, 0), which keeps the combination formulas valid without special cases.

House Robber III (Tree DP) Worked Example

Input: {values: [3, 2, 3, 3, 1]}

Level order builds the tree: root 3 with children 2 and 3; the 2 has children 3 and 1. Leaves report (3, 0) and (1, 0). At the 2, robbing it excludes both children: rob = 2, skip = max(3, 0) + max(1, 0) = 4, so the pair is (2, 4). The right 3 is a leaf: (3, 0). At the root, rob = 3 + 4 + 0 = 7 while skip = max(2, 4) + max(3, 0) = 7, so the answer is 7 — either take the root and the 2's children, or skip the root and take the 2 plus the right 3.

DP: House Robber III (Tree DP)O(n)
Each tree node returns (rob, skip); parent combines children.
3
2
3
3
1
Step 1 / 7
Default Current / Compare Swap / Write Settled Active / Path Pivot / Reject Locked (hollow) Target
House Robber III: houses form a tree — robbing a node blocks its children. Each node returns (rob, skip).
Python · Dynamic Programming Algorithms in Code
def rob(root):                 # returns (rob, skip)
    if not root: return (0, 0)
    l, r = rob(root.left), rob(root.right)
    return (root.val + l[1] + r[1],   # take: kids skipped
            max(l) + max(r))          # skip: best of kids

# answer = max(rob(root))  — O(n) post-order

Behind the Scenes Execution

Press ▶ Run in the visualizer — the call stack, registers and memory will replay here in sync with the code, line by line.

House Robber III (Tree DP) Time & Space Complexity

BestO(n)
AverageO(n)
WorstO(n), one visit per node
SpaceO(h) call stack for tree height h, O(n) only for a completely skewed tree

When to Use House Robber III (Tree DP)

  • Any selection problem on a tree where adjacent nodes conflict: scheduling dependent jobs, choosing independent cells in a hierarchy, or maximum-weight independent set on a tree.

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits

Keep the momentum going

Step through a related structure, pattern or course — the ideas reinforce each other and stick faster.