MediumDynamic ProgrammingTreeDepth-First SearchBinary TreeDP on TreesUpdated Sep 2026

337. House Robber III

Asked at Amazon

Difficulty

Medium

Time

O(n)

Space

O(h)

Pattern

Dynamic Programming

Companies

1

Problem

A thief wants to rob houses that are arranged as a binary tree. The root node is the only entrance. Each house holds some money, given by its node value.

The police are called if the thief robs two houses that are directly connected by an edge (a parent and its child). So the thief must pick a set of nodes where no two chosen nodes are adjacent.

Given the root of the tree, return the maximum total money the thief can take without triggering the alarm.

Pick nodes with no parent-child pair

23331Rob 3 + 3 + 1 = 7; no two green nodes touch.
The chosen houses (green) must never be directly connected by an edge.

Constraints

  • ●The number of nodes in the tree is in the range [1, 10^4].
  • ●0 <= Node.val <= 10^4

Examples

Example 1
Inputroot = [3,2,3,null,3,null,1]
Output7
WhyRob the root (3), the right child (3), and the far-right leaf (1). None of these are directly connected, and 3 + 3 + 1 = 7. Robbing the root blocks its children, but the grandchildren are still free.
Example 2
Inputroot = [3,4,5,1,3,null,1]
Output9
WhySkip the root (3) and rob its two children instead: 4 + 5 = 9. Taking the root would only give 3 plus whatever is deeper, which is less than 9.

Asked At

1 companies
CompanyDifficulty
AmazonMediumView all Amazon questions →

How to Think About It

1

Think recursively — for each node, you either rob it or skip it.

2

A naive recursive solution explores both choices at every node, leading to exponential time.

3

Use memoization to cache results of subtrees and avoid recomputation.

4

At each node, return two values: the max money if you rob this node, and the max if you skip it.

5

The optimal solution uses a single DFS pass returning a pair (rob, skip) for each subtree, achieving O(n) time.

Optimal Approach

Perform a depth-first search on the tree. For each node, return a pair (rob, skip) where rob is the max money obtainable if the node is robbed, and skip is the max if the node is skipped. If you rob the current node, you must skip its children: rob = node.val + left.skip + right.skip. If you skip the current node, you can take the best from children: skip = max(left) + max(right). The answer is max(root.rob, root.skip).

Each node returns (rob, skip)

2(2, 3)33(rob, skip) = (7, 6)3(4, 3)1Leaves return (value, 0); parents combine their children'spairs.rob = node.val + left.skip + right.skipskip = max(left) + max(right)answer = max(root.rob, root.skip)
The DFS returns two numbers per node: best if you rob it, best if you skip it.
ComplexityTime O(n)Space O(h)Each node is visited once during the DFS; h is the tree height and bounds the recursion stack.

What Trips People Up in Real Interviews

1

Clarify the constraint — adjacent means connected by an edge in the tree, not just neighboring levels.

2

Start by explaining the brute force recursive approach with two choices per node.

3

Transition to the optimized DFS that returns a tuple of (rob_this_node, skip_this_node).

4

Explain why this is O(n) time — each node is visited exactly once.

5

Edge cases: null tree (0), single node (value of that node), skewed tree vs balanced tree.

Solution Code

O(n) time
def rob(root):
    def dfs(node):
        if not node:
            return (0, 0)
        left = dfs(node.left)
        right = dfs(node.right)
        rob = node.val + left[1] + right[1]
        skip = max(left) + max(right)
        return (rob, skip)
    return max(dfs(root))

Pro at DSA?

Test your skills with a real FAANG-style mock interview.

Start a Mock Interview →

Frequently Asked Questions

What is the House Robber III problem?

A thief robs houses arranged in a binary tree where each node represents the amount of money in that house. No two adjacent houses (connected by an edge) can be robbed on the same night. Find the maximum amount that can be robbed.

How do you solve House Robber III?

The optimal approach runs in O(n) time and O(h) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.

What companies ask House Robber III?

House Robber III is asked at Amazon. It is a medium difficulty problem.

What are common mistakes on House Robber III?
  • Clarify the constraint — adjacent means connected by an edge in the tree, not just neighboring levels.
  • Start by explaining the brute force recursive approach with two choices per node.
  • Transition to the optimized DFS that returns a tuple of (rob_this_node, skip_this_node).
  • Explain why this is O(n) time — each node is visited exactly once.
  • Edge cases: null tree (0), single node (value of that node), skewed tree vs balanced tree.