337. House Robber III

  • Total Accepted: 18475
  • Total Submissions: 47725
  • Difficulty: Medium

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:

    / \

    \   \
        

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

Example 2:

    / \

  / \   \
       

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

思路:基本思路和Leetcode 198. House Robber相同,只是将传递公式植入到二叉树的DFS过程中。

代码:

 /**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
void rob(TreeNode* root,int& pre,int& cur){
if(!root) return;
int lpre=,lcur=,rpre=,rcur=;
rob(root->left,lpre,lcur);
rob(root->right,rpre,rcur);
pre=lcur+rcur;
cur=max(lpre+rpre+root->val,pre);
}
int rob(TreeNode* root) {
int pre=,cur=;
rob(root,pre,cur);
return max(pre,cur);
}
};

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

  1. [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 ...

  2. [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 ...

  3. Java [Leetcode 337]House Robber III

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

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

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

  5. 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:二叉树下的不能相邻,求能 ...

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

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

  7. <LeetCode OJ> 337. House Robber III

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

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

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

  9. 【LeetCode】House Robber III(337)

    1. Description The thief has found himself a new place for his thievery again. There is only one ent ...

随机推荐

  1. django model中的save()方法

    Model.save(force_insert=False, force_update=False, using=DEFAULT_DB_ALIAS, update_fields=None) id和pk ...

  2. Linux tomcat 添加开机启动

    准备工作:将 jdk-7u80-linux-x64.tar.gz 解压到到 /usr/local/目录下将 apache-tomcat-7.0.82.zip 解压到/opt/etcoud目录下,并切换 ...

  3. EF一对多的表,模糊查询2个表的数据!

    如用户表和电话表,要求搜索时可以模糊查询姓名和号码.都可以找到包含该字符的所有用户. /// <summary> /// 模糊查询姓名和电话号码,并按姓名排序返回 /// </sum ...

  4. 内置函数——sorted

    对List.Dict进行排序,Python提供了两个方法对给定的List L进行排序,方法1.用List的成员函数sort进行排序,在本地进行排序,不返回副本方法2.用built-in函数sorted ...

  5. 1305 Pairwise Sum and Divide(数学 ,规律)

    HackerRank   1305 Pairwise Sum and Divide   有这样一段程序,fun会对整数数组A进行求值,其中Floor表示向下取整:   fun(A)     sum = ...

  6. 【文文殿下】CF1029F Multicolored Markers

    这道题考场上卡了文文相当长的时间,所以写个题解泄泄愤QAQ 题意:给你$a$块红瓷砖,$b$块白瓷砖,在一个无限大的地上拼装,要求整体是一个矩形,并且至少有一种颜色是一个矩形,求最小周长. 题解: 首 ...

  7. IE下判断IE版本的语句...[if lte IE 6]……[endif](用户判断IE版本的如果小于6就显示)

    <!--[if lte IE 6]> <![endif]--> IE6及其以下版本可见   <!--[if lte IE 7]> <![endif]--> ...

  8. docker微服务部署之:四、安装docker、docker中安装mysql和jdk1.8、手动构建镜像、部署项目

    docker微服务部署之:三,搭建Zuul微服务项目 1.Centos7安装Docker 详见:Centos7安装Docker 2.Docker中安装jdk1.8 详见:使用Docker构建jdk1. ...

  9. URL中 # (hash)的含义

    url中#(hash)的含义 hash 属性是一个可读可写的字符串,该字符串是 URL 的锚部分(从 # 号开始的部分) 1."#"代表网页中的一个位置.其右面的字符,就是该位置的 ...

  10. 彻底理解JDK异步

    学而时习之,不亦说乎!                              --<论语> 首发,转载请附原文链接,谢谢. 原文使用MD格式编写,复制进来代码缩成一团了,读者见谅,需要 ...