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. VUE 学习笔记 四 计算属性和监听器

    1.计算属性 对于任何复杂逻辑,你都应当使用计算属性 <div id="example"> <p>Original message: "{{ me ...

  2. c#中sealed修饰符

    sealed 修饰符表示密封 用法: 1.用于类时,表示该类不能再被继承,不能和abstract同时使用,因为这两个修饰符在含义上互相排斥 2.用于方法和属性时,表示该方法或属性不能再被重写,必须和o ...

  3. 操作XDocument讲解

    1.首先建立好XML .可以通选自定义EXCEL导出XML格式的数据:(如图) 2 读取XML 文件 具体的详细讲解 可以查看 改网址 :https://blog.csdn.net/dyllove98 ...

  4. html Canvas 画图 能够选择并能移动

    canvas 画图,能够选中所画的图片并且能够随意移动图片 <html xmlns="http://www.w3.org/1999/xhtml"> <head r ...

  5. iOS 的各种识别码

    在iOS系统中,获取设备唯一标识的方法有很多: 1. x UDID (Unique Device Identifier,已过期) 2. x OPEN UDID (已过期) 3. 广告标示符(IDFA- ...

  6. 初学python之路-day15

    一.生成器send方法 # send的工作原理 # 1.send发送信息给当前停止的yield # 2.再去调用__next__()方法,生成器接着往下指向,返回下一个yield值并停止 # 案例: ...

  7. 红与黑(DFS)

    描述有一间长方形的房子,地上铺了红色.黑色两种颜色的正方形瓷砖.你站在其中一块黑色的瓷砖上,只能向相邻的黑色瓷砖移动.请写一个程序,计算你总共能够到达多少块黑色的瓷砖.输入包括多个数据集合.每个数据集 ...

  8. 【OCP-12c】CUUG 071题库考试原题及答案解析(17)

    17.(7-11) choose twoView the Exhibit and examine the structure of ORDER_ITEMS and ORDERS tables.You ...

  9. OC中NSClassFromString()与NSStringFromClass()的用法及应用场景

    1.NSClassFromString()利用一个字符串创建一个类,我是在标签控制器中 UITabBarController中创建它的子控制器中使用的 - (void)viewDidLoad { [s ...

  10. 获取 input type="file" 标签的内容,并使用ajax进行请求到服务器

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...