House Robber III
Problem
The thief has found himself a new place for his thievery again. There is only one entrance to this area, called the "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 forms a binary tree". It will automatically contact the police if two directly-linked houses were broken into on the same night.
Determine the maximum amount of money the thief can rob tonight without alerting the police.
Solution
Easy Recursive Solution
public class Solution {
public int rob(TreeNode root) {
if (root == null) return 0;
int robRoot = root.val;
if (root.left != null) robRoot += rob(root.left.left) + rob(root.left.right);
if (root.right != null) robRoot += rob(root.right.left) + rob(root.right.right);
return Math.max(robRoot, rob(root.left) + rob(root.right));
}
}
Analysis
This idea of this solution is pretty straightforward.
We know if we rob a node
, we cannot rob it's direct children (node.left
&& node.right
)
Therefore, we use recursion and return max of robbing the root or not
Since it will cover all situations, we guarantee our algorithm works
Beauty of Recursion!