Problem
The thief has found himself a new place for his thievery again. There is only one entrance to this area, called root
.
Besides the root
, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if two directly-linked houses were broken into on the same night.
Given the root
of the binary tree, return the maximum amount of money the thief can rob without alerting the police.
Examples
Example 1:
graph TD; C(3):::robbed --- B(2) & C1(3) B ~~~ N1:::hidden B --- C2(3):::robbed C1(3) ~~~ N2:::hidden C1 --- A(1):::robbed classDef hidden display: none; classDef robbed stroke:#f66,stroke-width:2px
|
|
Similar Problems
House Robber House Robber 2 - Houses in circle House Robber 4
Solution
Method 1 - DFS
Since this is a binary tree, we can traverse it recursively using post-order traversal and calculate the maximum amount of money the thief can rob for each subtree - when. a root is robbed and when it is not selected.
Approach:
- Use post-order traversal (left → right → root):
- Calculate the maximum loot for the left and right children recursively.
- For each node, two scenarios are possible:
- Node is robbed: In this case, the thief cannot rob the children of this node. The amount will be
node.val + max(left child when not robbed) + max(right child when not robbed)
. - Node is not robbed: The thief can rob its children. The amount will be
max(left child when robbed, left child when not robbed) + max(right child when robbed, right child when not robbed)
.
- Node is robbed: In this case, the thief cannot rob the children of this node. The amount will be
- Use a helper function that returns two values for each node:
- The first is the maximum loot when the current node is not robbed.
- The second is the maximum loot when the current node is robbed.
- Finally, at the root, return the maximum of these two values.
Code
|
|
|
|
Complexity
- ⏰ Time complexity:
O(n)
, wheren
is the number of nodes in the binary tree. Each node is visited once. - 🧺 Space complexity:
O(h)
, whereh
is the height of the tree. This is due to the recursion stack.