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. hdu 5056 Boring count (类似单调队列的做法。。)

    给一个由小写字母构成的字符串S,问有多少个子串满足:在这个子串中每个字母的个数都不超过K. 数据范围: 1<=T<= 1001 <= the length of S <= 10 ...

  2. 服务集与AP的配合

    一.实验目的 1)掌握添加无线网络配置 2)掌握配置信道和协议使用并配置在一个天线上同时运行两个服务集,即两个无线网络 二.实验仪器设备及软件 仪器设备:一台AC,两台AP,一台AR,一台LSW 软件 ...

  3. CentOS部署多台服务器JDK(shell脚本部署)

    部署7台新服务器的jdk,数量不算多,但也不打算一台一台的部署,写了个脚本执行 [ #!/bin/bash# JDK 安装包名jdk_packge="jdk-8u162-linux-x64. ...

  4. python语法与pycharm的基本使用

    内容概要 pycharm基本使用 python注释语法 变量与常量 垃圾回收机制 数据类型 1. pycharm基本使用 pycharm安装完成后首次打开要注意: 文件路径(不要选择C盘) pytho ...

  5. Pytorch中stack()方法的理解

    Torch.stack() 1. 概念 在一个新的维度上连接一个张量序列 2. 参数 tensors (sequence)需要连接的张量序列 dim (int)在第dim个维度上连接 注意输入的张量s ...

  6. springmvc学习笔记(全)

    SpringMVC简介 什么是MVC MVC是一种软件架构的思想,将软件按照模型.视图.控制器来划分 M: Model:模型层,指工程中的JavaBean,作用是处理数据.JavaBean分为两类: ...

  7. oxidized备份华为HRP防火墙配置失败问题

    Oxidized备份华为防火墙配置Last Status红色,备份失败,查看oxidized日志(默认是~/.config/oxidized/logs/oxidized.log)能看到报错日志: WA ...

  8. oracle的 listagg() WITHIN GROUP () 行转列函数的使用

    1.使用条件查询 查询部门为20的员工列表    -- 查询部门为20的员工列表    SELECT t.DEPTNO,t.ENAME FROM SCOTT.EMP t where t.DEPTNO ...

  9. SpringBoot Actuator — 埋点和监控

    项目中看到了有埋点监控.报表.日志分析,有点兴趣想慢慢捣鼓一下 1. 数据埋点 监控机器环境的性能和业务流程或逻辑等各项数据,并根据这些数据生成对应的指标,那么我们就称为数据埋点.比如我们想知道某个接 ...

  10. 一文详解 OpenGL ES 3.x 渲染管线

    OpenGL ES 构建的三维空间,其中的三维实体由许多的三角形拼接构成.如下图左侧所示的三维实体圆锥,其由许多三角形按照一定规律拼接构成.而组成圆锥的每一个三角形,其任意一个顶点由三维空间中 x.y ...