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;
}
};

最新文章

  1. C#~异步编程再续~await与async引起的w3wp.exe崩溃-问题友好的解决
  2. oracle的特殊权限s bit丢失
  3. 安装了VS2012 还有Update4 我的Silverlight5安装完后 我的Silverlight4项目打不开
  4. Dubbo与Zookeeper、SpringMVC整合和使用(负载均衡、容错)
  5. 正则表达式统计java代码空白行,有效代码
  6. js按Enter键提交表单
  7. 客户端接口AGENDA
  8. WinSetupFromUSB – Install Windows XP from USB Flash Drive
  9. HLG 2163 方格取数 (最大网络流)
  10. 201521123011 《Java程序设计》 第三周学习总结
  11. Java语言概论
  12. 最新版Navicat Premium12 中文破解版 安装激活
  13. Leetcode 1-10
  14. Vmware Vtop基本使用
  15. django组件:中间件
  16. JPEG图片扩展信息读取与改动
  17. 怎样用Java代码来把SSL的证书自己主动导入到Java的秘钥存储文件(keystore)
  18. 【BZOJ1065】【NOI2008】奥运物流(动态规划)
  19. oracle 的一些基础查询
  20. selenium - webdriver - ActionChains类(鼠标操作)

热门文章

  1. Docker配置tomcat端口映射后无法访问(404)
  2. “TCP:三次握手”分析——以一个简单的“服务器”和“客户端”为例
  3. pip 更新方法
  4. vue事件绑定
  5. .NET Conf 2021 正在进行中,带你看一看微软带来了什么内容
  6. R数据分析:生存分析与有竞争事件的生存分析的做法和解释
  7. [atAGC054D]ox
  8. [ARC117D]Miracle Tree
  9. 六、Java API操作zookeeper节点
  10. 虚拟机Centos7安装Socks5作为代理服务器