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.

对于这个题目我们有什么思路呢?我们从根节点开始有两种选择,要么偷取根节点的值,要么不偷取根节点。偷取根节点的话,我们接下来只能偷取根节点孩子节点的孩子节点。如果不偷取根节点的话,我们可以直接偷取根节点的孩子节点。然后我们把这两种方式得到的值进行比较,取较大的那个。因此这是一个递归的过程:

rob(root) = max{rob(root.left) + rob(root.rght), root.val + rob(root.left.left) + rob(root.left.right) + rob(root.right.left) + rob(root.right.right)}

if(root == null) rob(root) = 0;

if(root.left == null && root.right == null) rob(root) = root.val

有了上面的递归表达式,我们就很容易进行编程啦!代码如下:

 /**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public int rob(TreeNode root) {
if(root == null) return 0;
if(root.left == null && root.right == null) return root.val;
int maxnum1 = 0;
int maxnum2 = 0;
maxnum1 = rob(root.left) + rob(root.right);
if(root.left != null || root.right != null){
int num1 = root.left!=null?rob(root.left.left) + rob(root.left.right):0;
int num2 = root.right!=null?rob(root.right.left) + rob(root.right.right):0;
maxnum2 = num1 + num2 + root.val;
}
return maxnum1>maxnum2?maxnum1:maxnum2;
}
}

LeetCode OJ 337. House Robber III的更多相关文章

  1. <LeetCode OJ> 337. House Robber III

    Total Accepted: 1341 Total Submissions: 3744 Difficulty: Medium The thief has found himself a new pl ...

  2. Leetcode 337. House Robber III

    337. House Robber III Total Accepted: 18475 Total Submissions: 47725 Difficulty: Medium The thief ha ...

  3. leetcode 198. House Robber 、 213. House Robber II 、337. House Robber III 、256. Paint House(lintcode 515) 、265. Paint House II(lintcode 516) 、276. Paint Fence(lintcode 514)

    House Robber:不能相邻,求能获得的最大值 House Robber II:不能相邻且第一个和最后一个不能同时取,求能获得的最大值 House Robber III:二叉树下的不能相邻,求能 ...

  4. 337. House Robber III(包含I和II)

    198. House Robber You are a professional robber planning to rob houses along a street. Each house ha ...

  5. [LeetCode] 337. House Robber III 打家劫舍之三

    The thief has found himself a new place for his thievery again. There is only one entrance to this a ...

  6. [LeetCode] 337. House Robber III 打家劫舍 III

    The thief has found himself a new place for his thievery again. There is only one entrance to this a ...

  7. Java [Leetcode 337]House Robber III

    题目描述: The thief has found himself a new place for his thievery again. There is only one entrance to ...

  8. 【LeetCode】337. House Robber III 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  9. LeetCode 337. House Robber III 动态演示

    每个节点是个房间,数值代表钱.小偷偷里面的钱,不能偷连续的房间,至少要隔一个.问最多能偷多少钱 TreeNode* cur mp[{cur, true}]表示以cur为根的树,最多能偷的钱 mp[{c ...

随机推荐

  1. MD5的Hash长度扩展攻击

    Hash长度扩展攻击 引子 无意中碰到一道题,大概代码是这样的 $flag = "XXXXXXXXXXXXXXXXXXXXXXX"; $secret = "XXXXXXX ...

  2. TortoiseGit 错误信息Aborting commit due to empty commit message.解决

    错误信息: Aborting commit due to empty commit message. git不能完全退出(退出码 1) (47 ms @ 2016/2/19 14:03:24) 解决办 ...

  3. Linux之VI搜索相关命令

    /abc, 向前查询abc ?abc, 向后查询abc n, 向前继续查询 N, 向后继续查询 老是忘记,简单记录下

  4. .net技术深入

    程序集(Assembly),可以看做是一堆相关类打一个包,相当于java中的jar包(*).打包的目的:程序中只引用必须的程序集,减小程序的尺寸:一些程序集内部的类不想让其他程序集调用. 我们调用的类 ...

  5. 第一百零四节,JavaScript时间与日期

    JavaScript时间与日期 学习要点: 1.Date类型 2.通用的方法 3.格式化方法 4.组件方法 ECMAScript提供了Date类型来处理时间和日期.Date类型内置一系列获取和设置日期 ...

  6. MVC写在Model文件夹下,登录注册等页面定义的变量规则,不会被更新实体模型删除

    一下图为我的model文件夹

  7. Java中ArrayList的使用

    //创建ArrayList ArrayList arr  = new ArrayList(); //ArrayList添加数据 arr.add("123"); arr.add(&q ...

  8. 【Sort】RadixSort基数排序

    太晚了,明天有时间在写算法思路,先贴代码 ------------------------------------------------ 刚答辩完,毕业好难,感觉自己好水 ------------- ...

  9. POJ 3070 矩阵快速幂解决fib问题

    矩阵快速幂:http://www.cnblogs.com/atmacmer/p/5184736.html 题目链接 #include<iostream> #include<cstdi ...

  10. 1、SpringMVC+MyBaits实现查询所有

    1.创建如下所示项目 2.在src下的com.entity包下创建 Emp.java package com.entity; /** * * @author Holly老师 * */ public c ...