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:

     3
/ \
2 3
\ \
3 1

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

Example 2:

     3
/ \
4 5
/ \ \
1 3 1

Maximum amount of money the thief can rob = 4 + 5 = 9.

# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution(object):
def rob(self, root):
"""
:type root: TreeNode
:rtype: int
3 => 3
3 => 3
/
2
3
\
3 =>3=max(3,3)
3
/ \ => 3+2=5=max(3, 2+3)
2 3
3
/ \
2 3
\
3 => 3+3=6=max(3+3,2+3)=max(3+node2,3 not choose, node2,3 choosed)
1
/ \
4 1
/ \ \
1 1 5 => 4+5=9=max(3+3+1+1,4+5)=9
"""
return max(self.rob_helper(root)) def rob_helper(self, root):
if root is None:
return [0, 0]
ans = [0]*2
ans_left = self.rob_helper(root.left)
ans_right = self.rob_helper(root.right)
ans[0] = max(ans_left[0], ans_left[1]) + max(ans_right[0], ans_right[1])
ans[1] = ans_left[0] + ans_right[0] + root.val
return ans

337. House Robber III——树的题目几乎都是BFS、DFS,要么递归要么循环的更多相关文章

  1. Leetcode 337. House Robber III

    337. House Robber III Total Accepted: 18475 Total Submissions: 47725 Difficulty: Medium The thief ha ...

  2. 337. House Robber III(包含I和II)

    198. House Robber You are a professional robber planning to rob houses along a street. Each house ha ...

  3. 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:二叉树下的不能相邻,求能 ...

  4. [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 ...

  5. Java [Leetcode 337]House Robber III

    题目描述: The thief has found himself a new place for his thievery again. There is only one entrance to ...

  6. [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 ...

  7. LeetCode OJ 337. House Robber III

    The thief has found himself a new place for his thievery again. There is only one entrance to this a ...

  8. 337. House Robber III二叉树上的抢劫题

    [抄题]: The thief has found himself a new place for his thievery again. There is only one entrance to ...

  9. LeetCode 337. House Robber III 动态演示

    每个节点是个房间,数值代表钱.小偷偷里面的钱,不能偷连续的房间,至少要隔一个.问最多能偷多少钱 TreeNode* cur mp[{cur, true}]表示以cur为根的树,最多能偷的钱 mp[{c ...

随机推荐

  1. MySQL子查询(SubQuery)

    由比较运算符引发的子查询,若括号内的子查询结果为多个,要在括号前加上ANY/SOME/ALL 由[NOT]IN引发的子查询, =ANY与IN等效       !=ALL / <>ALL与N ...

  2. CI实践_Android持续集成

    之前已经实现了Android的持续集成,并在项目中应用了一段时间.恰逢现在有几分钟时间,把之前的一些零散的点滴记录和整理一下,供有需要的朋友参考,或后续复用. 需要的准备知识:gitlab.Jenki ...

  3. iOS - UIDevice

    前言 NS_CLASS_AVAILABLE_IOS(2_0) @interface UIDevice : NSObject @available(iOS 2.0, *) public class UI ...

  4. spring事务知识

    事务的传播行为? 在Spring 的事务中, _可以通过 propagation 来定义事务的传播行为_: PROPAGATION_required:如果当前没有事务,就新建一个事务,如果已经存在一个 ...

  5. css3动画导航实现

    代码 <!DOCTYPE html> <!-- saved from url=(0223)file:///C:/Users/user/AppData/Local/Temp/HZ$D. ...

  6. poj1266Cover an Arc(三角形外接圆)

    链接 求出三角形的外接圆,通过圆心和半径可以知道这个圆的上下左右最远点,分别判断这个四个点跟弧的两端点A,B的关系,假如判断P点,弧内给出点为C,判断PC是否与AB相交即可判断出P是否在弧上. 精度问 ...

  7. Maven——聚合与继承

    原文:http://www.cnblogs.com/xdp-gacl/p/4058008.html 一.聚合 如果我们想一次构建多个项目模块,那我们就需要对多个项目模块进行聚合 1.1.聚合配置代码 ...

  8. haskell读写文件相关(含二进制)

    使用System.IO模块 使用函数 openBinaryFile :: FilePath -> IOMode -> IO Handle 打开文件 IOMode为 ReadWriteMod ...

  9. SQL数据库基本操作语句

    一.数据库及数据库表定义 1.创建基本表 create table <表名> (<列名><数据类型>[列级完整性约束条件]                      ...

  10. window下安装nodejs

    下载nodejs 去https://nodejs.org/en/download/下载nodejs,有.mis(安装版)和.exe(二进制版) .mis(安装版) 一般下载这个就行,简单方便,自带np ...