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. struts中request传递中文乱码问题

    系统本来是好好地,这两天升级后,各种问题不断,总而言之,一句话,心惊胆战. 今天,搜索任何中文,都是有乱码,在action中转码就ok了.公司系统那么多action,都转码,要累死吧.配置的过滤器都不 ...

  2. centos7 安装SSH

    1.安装OpenSSH服务(CentOS系统默认安装了openssh)      yum install openssh-server -y 2.配置OpenSSH服务(默认的配置已可以正常工作) O ...

  3. linux bash变量替换(# ## % %% / //)

    VAR=hahaha echo ${VAR#*h} # ahaha 从前向后匹配删除 VAR=hahaha echo ${VAR##*h} # a 贪婪模式,从前向后匹配删除所有 VAR=hahaha ...

  4. windows下用wampServer 为wordpress 搭建本地服务器运行环境

      1.准备wamp server wamp是windows apache mysql php 的首字母缩写,更新的wamp总能保证wordpress对服务器的要求 点此下载最新wamp 2.安装wa ...

  5. 知物由学 | AI网络安全实战:生成对抗网络

    本文由  网易云发布. “知物由学”是网易云易盾打造的一个品牌栏目,词语出自汉·王充<论衡·实知>.人,能力有高下之分,学习才知道事物的道理,而后才有智慧,不去求问就不会知道.“知物由学” ...

  6. python网络编程--线程的方法,线程池

    一.线程的其他方法(Thread其他属性和方法) ident() 获取线程id Thread实例对象的方法 isAlive() 设置线程名 getName() 返回线程名 setName() 设置线程 ...

  7. GO学习笔记 - 函数名前面是否有输入参数肯定是不一样的!!

    在刚接触GO语言时候,我相信你也会有这种困惑,为什么有的函数名前面有输入参数,而一些却没有,它们是否有差别?确实有差别,没有输入参数,是一般的函数:有输入参数,是结构的方法,输入参数叫做“方法接收者” ...

  8. java中的引用类型 部分讲解

    所谓的引用类型 类--接口--数组--枚举 [01--Scanner类] Scanner 这个类是用于键盘输入 它的格式为 类型  对象名称 =  new  类型(): 它的操作格式  对象名.nex ...

  9. python list 嵌套 dict 按照字典中的单个key进行单级排序 或 按照多个键进行多级排序

    student = [{"no": 1,"score": 90},{"no": 2,"score": 90},{&quo ...

  10. Java Web 学习与总结(一)Servlet基础

    配置环境:https://www.cnblogs.com/qq965921539/p/9821374.html 简介: Servlet是Sun公司提供的一种实现动态网页的解决方案,在制定J2EE时引入 ...