The problem “House Robber III” can be found on LeetCode:
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.
Example:
Input: [3,2,3,null,3,null,1]
3
/ \
2 3
\ \
3 1
Output: 7
Explanation: Maximum amount of money the thief can rob = 3 + 3 + 1 = 7.
To solve this problem, we can use a recursive approach with memoization to enhance the performance:
Here’s a Python implementation of the strategy described:
# Definition for a binary tree node.
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
class Solution:
def rob(self, root: TreeNode) -> int:
def helper(node):
if not node:
return (0, 0)
# Post-order traversal to solve the problem bottom-up
left = helper(node.left)
right = helper(node.right)
# If we rob the current node, we cannot rob its direct children
rob_current = node.val + left[1] + right[1]
# Otherwise, we take the max of robbing or not robbing each child
not_rob_current = max(left) + max(right)
return (rob_current, not_rob_current)
# Calculate the maximum money that can be robbed starting from root
return max(helper(root))
# Example Usage
# Construct the example tree [3,2,3,null,3,null,1]
root = TreeNode(3)
root.left = TreeNode(2)
root.right = TreeNode(3)
root.left.right = TreeNode(3)
root.right.right = TreeNode(1)
solution = Solution()
print(solution.rob(root)) # Output: 7
O(n)
, where n
is the number of nodes in the binary tree. Each node is visited once.O(n)
, where n
is the number of nodes in the binary tree due to the recursion stack in the worst case (e.g., in a skewed tree).This solution efficiently calculates the maximum amount of money that can be robbed following the given constraints.
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?