337. House Robber III

探讨了一个经典的二叉树遍历问题:如何在不触动报警的情况下,从二叉树结构的房屋中抢夺最大金额。文章提供了三种解决方案,包括贪心算法、动态规划以及简化版动态规划,详细解释了每种方法的实现思路与复杂度。
Description

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.

Example 1:

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.
Example 2:

Input: [3,4,5,1,3,null,1]

 3
/ \

4 5
/ \ \
1 3 1

Output: 9
Explanation: Maximum amount of money the thief can rob = 4 + 5 = 9.

Problem URL


Solution

给一棵二叉树,只能隔一个抢一个,问最多能抢到多少钱。

Approach 1: Greedy method. Since we want to get the most money start from root, we also want to do it at left subtree and right subtree. Thus, we could solve this problem recursively.

So the termination codition could be root == null, since this path is end. For the recurrence relation, it is depend on root. If rob root, we have to rod grandchild. If not, just child. So we could get the recursion function.

But this solution is quite slow. It runs 771ms.

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public int rob(TreeNode root) {
        if (root == null){
            return 0;
        }
        int val = 0;
        if (root.left != null){
            val += rob(root.left.left) + rob(root.left.right);
        }
        if (root.right != null){
            val += rob(root.right.left) + rob(root.right.right);
        }
        return Math.max(val + root.val, rob(root.left) + rob(root.right));
    }
}

Time Complexity: O(2^n)
Space Complexity: O(n)

Approach 2: Dynamic programming. Because we have to calculate overlaping value, we could use dynamic programming to get easier approach. Use a hash map to record the results for visited subtrees.

Code
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public int rob(TreeNode root) {
        return robHelper(root, new HashMap<TreeNode, Integer>());
    }
    
    private int robHelper(TreeNode node, Map<TreeNode, Integer> map){
        if (node == null){
            return 0;
        }
        if (map.containsKey(node)){
            return map.get(node);
        }
        int val = 0;
        if (node.left != null){
            val += robHelper(node.left.left, map) + robHelper(node.left.right, map);
        }
        if (node.right != null){
            val += robHelper(node.right.left, map) + robHelper(node.right.right, map);
        }
        
        val = Math.max(val + node.val, robHelper(node.left, map) + robHelper(node.right, map));
        map.put(node, val);
        return val;
    }
}

Time Complexity: O(n)
Space Complexity: O(n)


Review

Approach 3: We just have two scenarios, so we could simply using a array with two element to store the output of each node.

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public int rob(TreeNode root) {
        int[] res = robHelper(root);
        return Math.max(res[0], res[1]);
    }
    
    private int[] robHelper(TreeNode node){
        if (node == null){
            return new int[2];
        }

        int[] left = robHelper(node.left);
        int[] right = robHelper(node.right);
        int[] res = new int[2];
        
        res[0] = Math.max(left[0], left[1]) + Math.max(right[0], right[1]);
        res[1] = node.val + left[0] + right[0];
        return res;
    }
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值