leetcode oj java 337. House Robber III

本文探讨了一个基于二叉树结构的问题,即如何在不触动警报的情况下,窃贼能够从连接成二叉树形状的房屋中盗取最多的财物。通过动态规划的方法,我们为每个节点定义了取与不取两种状态,最终求得最大盗取价值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、问题描述:

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:

     3
    / \
   2   3
    \   \ 
     3   1
Maximum amount of money the thief can rob =  3  +  3  +  1  =  7 .

Example 2:

     3
    / \
   4   5
  / \   \ 
 1   3   1
Maximum amount of money the thief can rob =  4  +  5  =  9 .

Credits:
Special thanks to @dietpepsi for adding this problem and creating all test cases.

Subscribe to see which companies asked this question


二、解决思路:

      动态规划的思想:每一个节点都有取和不取两种情况,用一个int 的数组来记录这两种情况的结果:re[0] 不取,re[1]取。 如果不取的话,re[0]就是左右孩子节点最大值,如果取得话,就是root.val 加上不取左右孩子节点的情况。


三、代码:


package T01;

/**
 * @author 作者 : xcy
 * @version 创建时间:2017年1月13日 下午12:26:00
 *          类说明
 */
public class t337 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        //        TreeNode root = new TreeNode(3);
        //        TreeNode r1 = new TreeNode(4);
        //        TreeNode r2 = new TreeNode(5);
        //        TreeNode r3 = new TreeNode(1);
        //        TreeNode r4 = new TreeNode(3);
        //        TreeNode r5 = new TreeNode(1);
        //        root.left = r1;
        //        root.right = r2;
        //        r1.left = r3;
        //        r1.right = r4;
        //        r2.right = r5;

        TreeNode root = new TreeNode(3);
        TreeNode r1 = new TreeNode(2);
        TreeNode r2 = new TreeNode(3);
        TreeNode r3 = new TreeNode(3);
        TreeNode r4 = new TreeNode(1);
        root.left = r1;
        root.right = r2;
        r1.right = r3;
        r2.right = r4;

        System.out.println(rob(root));
    }

    public static int rob(TreeNode root) {
        int[] re = SubRob(root);
        return Math.max(re[0], re[1]);
    }

    public static int[] SubRob(TreeNode root) {
        if (root == null) {
            return new int[2];
        }
        int[] left = SubRob(root.left);
        int[] right = SubRob(root.right);
        int[] re = new int[2];
        // re[0] not rob the root:
        re[0] = Math.max(left[0], left[1]) + Math.max(right[0], right[1]);
        // re[1] rob the root;
        re[1] = root.val + left[0] + right[0];
        return re;
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值