Maximum loot from a binary tree of houses when a parent and its child cannot both be robbed

Quick Overview

Houses form a binary tree, and robbing a house and its direct parent on the same night sets off the alarm. Return the largest total that can be taken. It tests dynamic programming on trees, post-order traversal, and handling deep, skewed trees without recursion errors.

Maximum loot from a binary tree of houses when a parent and its child cannot both be robbed

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

A thief has found a new neighborhood to rob. The houses are arranged as a binary tree: the only entrance is the house at the `root`, and every other house has exactly one parent house. Each node's value is the amount of money in that house. If two directly linked houses (a parent and its child) are both broken into on the same night, the alarm goes off and the police are called. Given the `root` of the tree, return the maximum amount of money the thief can rob without alerting the police. ### Function Signature ```python def rob(root: Optional[TreeNode]) -> int: ``` ### Rules - `root` is a `TreeNode` with fields `val`, `left` and `right`. A missing child is `None`. - The examples write the tree as a level-order list in which `None` marks a missing child. Trailing `None` values are left out. - Any set of houses may be robbed as long as no robbed house is the parent of another robbed house. The answer is the largest possible total. ### Constraints - The number of nodes is between 1 and 10^4. - `0 <= Node.val <= 10^4` ### Examples ```text Input: root = [3, 2, 3, None, 3, None, 1] Output: 7 ``` The tree has root 3, children 2 and 3, a 3 under the left child and a 1 under the right child. Robbing the root and the two bottom houses (3 + 3 + 1) gives 7, and none of those three houses are directly linked. ```text Input: root = [3, 4, 5, 1, 3, None, 1] Output: 9 ``` Robbing the root's two children gives 4 + 5 = 9. Robbing the root instead rules out both children, and the best total that way is 3 + 1 + 3 + 1 = 8. ```text Input: root = [2, 1, 3, None, 4] Output: 7 ``` The root 2 has children 1 and 3, and 4 hangs under the 1. The best choice is houses 4 and 3. It skips both the root and its left child, so taking every other level (2 + 4 = 6, or 1 + 3 = 4) falls short.

Overview: Houses form a binary tree, and robbing a house and its direct parent on the same night sets off the alarm. Return the largest total that can be taken. It tests dynamic programming on trees, post-order traversal, and handling deep, skewed trees without recursion errors.

PracHub logo
General
Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

A thief has found a new neighborhood to rob. The houses are arranged as a binary tree: the only entrance is the house at the root, and every other house has exactly one parent house. Each node's value is the amount of money in that house.

If two directly linked houses (a parent and its child) are both broken into on the same night, the alarm goes off and the police are called.

Given the root of the tree, return the maximum amount of money the thief can rob without alerting the police.

Function Signature

def rob(root: Optional[TreeNode]) -> int:

Rules

  • root is a TreeNode with fields val , left and right . A missing child is None .
  • The examples write the tree as a level-order list in which None marks a missing child. Trailing None values are left out.
  • Any set of houses may be robbed as long as no robbed house is the parent of another robbed house. The answer is the largest possible total.

Constraints

  • The number of nodes is between 1 and 10^4.
  • 0 <= Node.val <= 10^4

Examples

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

The tree has root 3, children 2 and 3, a 3 under the left child and a 1 under the right child. Robbing the root and the two bottom houses (3 + 3 + 1) gives 7, and none of those three houses are directly linked.

Input: root = [3, 4, 5, 1, 3, None, 1]
Output: 9

Robbing the root's two children gives 4 + 5 = 9. Robbing the root instead rules out both children, and the best total that way is 3 + 1 + 3 + 1 = 8.

Input: root = [2, 1, 3, None, 4]
Output: 7

The root 2 has children 1 and 3, and 4 hangs under the 1. The best choice is houses 4 and 3. It skips both the root and its left child, so taking every other level (2 + 4 = 6, or 1 + 3 = 4) falls short.