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:

  1. 3
  2. / \
  3. 2 3
  4. \ \
  5. 3 1

Maximum amount of money the thief can rob = 3 + 3 + 1 = 7.

题目大意

给定一二叉树,树节点上有权值,从树中选取一些不直接相邻的节点,使得节点和最大

思考过程

1. 简单粗暴,搜索完事,一个节点不外乎两种情况,选择 or 不选择;另外当前节点选择之后,子节点不能被选择;当前节点若不选择,则又可以分为四种情况

* 左选择,右不选

* 右选,左不选

* 左右都选

* 左右都不选

2. 写代码,好的然而超时(当然-_-后来看了其他的解答,才发现too young)

3. 因为看到有重复计算,于是朝动态规划考虑,于是想出这么个状态

d[0][1],其中0表示存储遍历到当前节点时,取当前节点能达到的最大值,而1则表示,不取当前节点能达到的最大值

又因为是树节点,所以直接哈希表存储d[TreeNode*][]

4. 遍历顺序呢,习惯性后序遍历

5. 计算规则

// 显然,当前节点取了,子节点不能取

d[TreeNode*][0] = TreeNodeVal + d[LeftChild][1] + d[RightChild][1]

// 四种情况

d[TreeNode*][1] = max(d[LeftChild][0] + d[RightChild][0], d[LeftChild][1] + d[RightChild][0], d[LeftChild][0] + d[RightChild][1], d[LeftChild][1] + d[RightChild][1])

6. 总算过了,附代码;看了讨论的思路之后,觉得真是too young,╮(╯▽╰)╭

  1. class Solution {
  2. public:
  3. int rob(TreeNode* root) {
  4. if (root == NULL) {
  5. return ;
  6. }
  7.  
  8. postOrder(root);
  9. return max(d[root][], d[root][]);
  10. }
  11.  
  12. void postOrder(TreeNode* itr) {
  13. if (itr == NULL) {
  14. return;
  15. }
  16.  
  17. postOrder(itr->left);
  18. postOrder(itr->right);
  19.  
  20. auto dItr = d.insert(pair<TreeNode*, vector<int>>(itr, vector<int>(, )));
  21. auto leftItr = dItr.first->first->left;
  22. auto rightItr = dItr.first->first->right;
  23.  
  24. int rL = dItr.first->first->left != NULL ? d[dItr.first->first->left][] : ;
  25. int rR = dItr.first->first->right != NULL ? d[dItr.first->first->right][] : ;
  26. int uL = dItr.first->first->left != NULL ? d[dItr.first->first->left][] : ;
  27. int uR = dItr.first->first->right != NULL ? d[dItr.first->first->right][] : ;
  28.  
  29. dItr.first->second[] = dItr.first->first->val + uL + uR;
  30. dItr.first->second[] = max(max(max(rL + uR, uL + rR), rL + rR), uL + uR);
  31. }
  32.  
  33. private:
  34. unordered_map<TreeNode*, vector<int>> d;
  35. };

借鉴DP思想: HouseRobberIII的更多相关文章

  1. 到底什么是dp思想(内含大量经典例题,附带详细解析)

    期末了,通过写博客的方式复习一下dp,把自己理解的dp思想通过样例全部说出来 说说我所理解的dp思想 dp一般用于解决多阶段决策问题,即每个阶段都要做一个决策,全部的决策是一个决策序列,要你求一个 最 ...

  2. hdu 3030 Increasing Speed Limits (离散化+树状数组+DP思想)

    Increasing Speed Limits Time Limit: 2000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java ...

  3. DP思想在斐波那契数列递归求解中的应用

    斐波那契数列:1, 1, 2, 3, 5, 8, 13,...,即 f(n) = f(n-1) + f(n-2). 求第n个数的值. 方法一:迭代 public static int iterativ ...

  4. hdu 4612 Warm up 双连通+树形dp思想

    Warm up Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total S ...

  5. DP思想笔记

    一.思想 DP也是把复杂的问题分解为许多子问题,与分治法不同的是,分治法的各个子问题互相之间没有联系,而动态规划却有.前一个子问题的结果与下一步的子问题的结果是什么有关系.这就决定了DP算法肯定有一个 ...

  6. 借鉴Glide思想二次封装Fresco

    本篇文章已授权微信公众号 dasu_Android(大苏)独家发布 最近封装了个 Fresco 的组件库:DFresco,就顺便来讲讲. 背景 Fresco 图片库很强大,我们项目中就是使用的 Fre ...

  7. CodeForces5E 环转链,dp思想

    http://codeforces.com/problemset/problem/5/E 众所周知,在很久以前,在今天的 Berland 地区,居住着 Bindian 部落.他们的首都被 n 座山所环 ...

  8. DAG上dp思想

    DAG上DP的思想 在下最近刷了几道DAG图上dp的题目.要提到的第一道是NOIP原题<最优贸易>.这是一个缩点后带点权的DAG上dp,它同时规定了起点和终点.第二道是洛谷上的NOI导刊题 ...

  9. 滑雪(经典DP思想)

    个人心得:思想还是不够,开始自己写但是不知道如何记录长度,也不太知道状态的转移,后面看了百度, 发现人人为我我为人人就是一步一步推导, 而递归思想就要求学会记录和找到边界条件,这一题中的话就是用递归, ...

随机推荐

  1. eclipse 32位和64位的jre

    让32位Eclipse和64位Eclipse同是在64的Windows7上运行 用文本编辑器打开eclipse.ini文件,在-vmargs之前加入下面的内容: -vm  C:\Program Fil ...

  2. open Session In View模式

    首先看图说话: ****Open Session In View模式的主要思想是:在用户的每一次请求过程始终保持一个Session对象打开着*** 接下来就是代码: +++++++++++++++++ ...

  3. Haskell之Yesod开发–边踩坑边开发(2)

    今天继续上一节的开发 今天我们须要详细的开发一个图书馆站点,分为下面几个页面 / HomeR GET 主页 /login LoginR GET 用户登录页面 /library LibraryR GET ...

  4. unicode下各种类型转换,CString,string,char*,int,char[]

    把最近用到的各种unicode下类型转换总结了一下,今后遇到其他的再补充: 1.string转CString string a=”abc”; CString str=CString(a.c_str() ...

  5. Linux设备驱动——内核定时器

    内核定时器使用 内核定时器是内核用来控制在未来某个时间点(基于jiffies)调度执行某个函数的一种机制,其实现位于 <Linux/timer.h> 和 kernel/timer.c 文件 ...

  6. 字典与集合(Dictionary与Collection)

    Dictionary对象将替换Collection对象,并提供附加的语言从而使增加和删除记录的速度比以前提高三倍 虽然Visual Basic 6.0只有很少的新特点,但是具有某些功能强大的新的对象模 ...

  7. svn和git比较

    svn有哪些优点和缺点? git有哪些优点和缺点? git最突然的优点就是gitflow,开发新的功能都是开一个新分支feature,完成开发新特性,合并到develop分支:提交测试也是新增一个分支 ...

  8. 使用linux操作系统的公司服务器有哪些品牌

    服务器硬件是什么牌子的? 操心系统有哪些?cpu是哪些?

  9. How to Build CyanogenMod for One X (codename: endeavoru)

    来源:http://wiki.cyanogenmod.org/w/Build_for_endeavoru#What_you.E2.80.99ll_need How to Build CyanogenM ...

  10. bash:ifconfig command not found for contos7.0

    CentOS7刚发布,我忍不住把DELL T410从CentOS6升级到CentOS7.好不容易等安装结束后,立即配置网络,然后在yum源上安装环境.可是执行ifconfig的时候系统提示让我傻了眼: ...