The thief has found himself a new place for his thievery again. There is only one entrance to this area, called 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 form a binary tree. It will automatically contact the police if two directly-linked houses were broken into on the same night.

Given the root of the binary tree, return the maximum amount of money the thief can rob without alerting the police.

Example 1:

class Solution {
private:
int res=0;
public:
int rob(TreeNode* root) {
// 二叉树的动态规划
// 首先需要遍历二叉树 然后计算最大的偷盗金额 用递归来遍历二叉树 用数组来存储状态
//dp[0,1] dp[0] 包含当前节点的最大值 ;dp[1] 不包含当前节点的最大值
vector<int>dp=digui(root);
return max(dp[0],dp[1]); }
vector<int> digui(TreeNode*node){
vector<int> dp(2,0);
if(node==nullptr) return dp;
vector<int> left=digui(node->left);
vector<int> right=digui(node->right);
dp[0]=left[1]+right[1]+node->val; //从最底层往上算
dp[1]=max(left[0],left[1])+max(right[0],right[1]); return dp;
}
};

【leetcode】337. House Robber III的更多相关文章

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

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

  2. 【LeetCode】732. My Calendar III解题报告

    [LeetCode]732. My Calendar III解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/my-calendar ...

  3. 【LeetCode】170. Two Sum III – Data structure design

    Difficulty:easy  More:[目录]LeetCode Java实现 Description Design and implement a TwoSum class. It should ...

  4. 【LeetCode】198. House Robber 打家劫舍 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 递归 + 记忆化 动态规划 优化动态规划空间 ...

  5. 【LeetCode】213. House Robber II 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/house-rob ...

  6. 【LeetCode】198 - House Robber

    You are a professional robber planning to rob houses along a street. Each house has a certain amount ...

  7. 【leetcode】437. Path Sum III

    problem 437. Path Sum III 参考 1. Leetcode_437. Path Sum III; 完

  8. 【LeetCode】216. Combination Sum III

    Combination Sum III Find all possible combinations of k numbers that add up to a number n, given tha ...

  9. 【LeetCode】213. House Robber II

    House Robber II Note: This is an extension of House Robber. After robbing those houses on that stree ...

随机推荐

  1. 【Go语言学习笔记】Go的defer

    关键字 defer ⽤于延迟一个函数或者方法(或者当前所创建的匿名函数)的执行. 注意,defer语句只能出现在函数或方法的内部. defer语句经常被用于处理成对的操作,如打开.关闭.连接.断开连接 ...

  2. float32 和 float64

    float32 和 float64 Go语言中提供了两种精度的浮点数 float32 和 float64. float32,也即我们常说的单精度,存储占用4个字节,也即4*8=32位,其中1位用来符号 ...

  3. 前端yyyy-mm-dd格式 计算一段工作日后,日期

    //计算一段工作日后,日期getWorkday(dat, itervalByDay) { function formatTen(f) { if (parseInt(f, 10) < 10) { ...

  4. [loj6498]农民

    对每一个节点用二元组$(p,v)$表示,其中$p$是其是父亲的左(0)还是右(1)儿子,$v$是其父亲的点权 $x$合法当且仅当:对于其到根路径上所有$(0,v)$都有$a_{x}<v$.$(1 ...

  5. mybatis-参数如何测试

    mybatis参数非常多测试的时候定位bug一直是个问题,如果老用大部分时间来定位一个错误的参数,太浪费时间了...

  6. Java ArrayList小记

    1.基本用法 ArrayList是一个泛型容器,新建ArrayList需要实例化泛型参数,比如: ArrayList<String> StrList = new ArrayList< ...

  7. Kubernetes:了解 Deployment

    本文为作者的 Kubernetes 系列电子书的一部分,电子书已经开源,欢迎关注,电子书浏览地址: https://k8s.whuanle.cn[适合国内访问] https://ek8s.whuanl ...

  8. 超图GIS入门iserver搭建,前端调用iserver加载三维场景demo

    目录 前言 一.GIS介绍,为什么选择它? 二.环境安装 三.调用三维GIS场景 设置地图风格 添加地图iServer服务 前言 前段时间因为对3D制图感兴趣,学习了一下国内制作GIS的公司产品技术, ...

  9. CF1554E You

    考虑到删点操作的实质是指认边的方向. 由于这是一棵树,所以有很好的性质. 我们完全可以以此从树叶开始,往上拓扑进行,按照对某个数的取膜的大小来进行操作. 由此可知,除了 \(1\) 以外,任意 \(2 ...

  10. IDE 常用配置

    启动进入欢迎页(项目选择页),而非直接进入项目 File > Settings > Appearance & Behavior > System Settings 在Star ...