2Brute2Best
πŸ”₯0

House Robber III

MediumΒ·LeetCode #337 Pro OnlyMedium
LeetCode
01

Problem Statement

A thief is robbing a row of houses that are arranged as a binary tree. The root is the entrance. Every other house has exactly one parent house.

Each house holds some money. The thief cannot rob two houses that are directly linked, meaning a house and one of its children. Two siblings, or a grandparent and a grandchild, are not directly linked, so both can be robbed.

Return the most money the thief can take in one night.

Input

The root of a binary tree. It has between 1 and 10^4 nodes. Each node's value is the money in that house.

Output

The largest total the thief can rob without taking two directly linked houses.

Example 1
Input: root = [3, 2, 3, null, 3, null, 1]
Output: 7

Rob the root (3), the 3 under the left child (3), and the 1 under the right child. That is 3 + 3 + 1 = 7. None of these three houses is a parent of another one.

Example 2
Input: root = [3, 4, 5, 1, 3, null, 1]
Output: 9

Rob the two children of the root: 4 and 5. They are siblings, so they are not directly linked. Total 4 + 5 = 9.

Constraints
  • β€’ The number of nodes is in the range [1, 10^4].
  • β€’ 0 ≀ Node.val ≀ 10^4
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

β€œrow of houses arranged as a binary tree”
β†’ Each house has a parent and up to two children. The links decide which choices conflict.
β€œcannot rob two directly linked houses”
β†’ A house and its child conflict. Siblings and grandparent-grandchild pairs do not conflict.
β€œmost money”
β†’ Maximise a sum over choices. Each house is either robbed or skipped.
03

Approach Complexity Ladder

Brute Force β€” plain recursion on rob(node)O(2^N)O(H)

rob(node) = max(value + grandchildren, children), with no memory

Better β€” same recursion with a memo mapO(N)O(N)

Save each node's answer in an unordered_map, so each node is solved once

Optimal β€” return two values per house (take, skip)O(N)O(H)

One post-order pass: each house returns best-if-robbed and best-if-skipped, no map

Brute Force Intuition

For one house, there are two choices. Rob it: take its value and add the best from its grandchildren, because the children are off-limits. Skip it: add the best from its two children, which are free. Return the larger of the two. The same subtrees are solved again and again.

Recognition rule for this sub-pattern:

Each node returns a summary of its subtree (height, best path). The global answer is updated while combining.
Remember this Β· Tree DFS
Trigger
Paths, heights, diameters, validating structure, ancestors, anything "for every subtree".
Think
If my left and right children already gave me their answers, how do I build mine?
Key question
What does my recursive function return, and what do I pass down?
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Tree DFS translate into production architectures and low-level memory mechanics.