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. SQL Server 小技巧【2】

    --1.不要使用×来查询所有字段 SELECT * FROM DBO.tb1 --改为 SELECT FName,PWD,CreateDate FROM DBO.tb1 (NOLOCK) --2.查询 ...

  2. Android中四种OnClick事件的写法

    package com.example.dailphone; import android.support.v7.app.ActionBarActivity; import android.suppo ...

  3. NOJ 1063 生活的烦恼

    描述 生活的暑假刚集训开始,他要决心学好字典树,二叉树,线段树和各种树,但生活在OJ上刷题的时候就遇到了一个特别烦恼的问题.那当然就是他最喜欢的二二叉树咯!题目是这样的:给你一颗非空的二叉树,然后再给 ...

  4. 转-Android Studio *.jar 与 *.aar 的生成与*.aar导入项目方法

    主要讲解Android Studio中生成aar文件以及本地方式使用aar文件的方法. 在Android Studio中对一个自己库进行生成操作时将会同时生成*.jar与*.aar文件. 分别存储位置 ...

  5. FileCopy

    /*[入]指的是到内存里,[出]指的是到内存外*/ import java.io.*; public class MyReadFile{ public static void main(String[ ...

  6. U盘安装XP_sp3

    1. 用的是老毛桃的 U盘制作工具(百度云OsSkill --> 全部文件 --> 软件安装包 --> 老毛桃 --> Install_LMT_v9_2__Win_x86.ex ...

  7. Python学习(15)文件/IO

    目录 Python 文件I/O 打印到屏幕 读取键盘输入 打开和关闭文件 File对象属性 文件定位 重命名和删除文件 Python的目录 Python 文件I/O 本章只讲述所有基本的的I/O函数, ...

  8. 用CSS样式截取字符串,多的用省略号表示

    <html><head><meta http-equiv="Content-Type" content="text/html; charse ...

  9. 【Todo】OSGi学习

    经常听到.见到OSGi这个名字.那么就单开一篇文章记录一下对OSGi的学习吧. 主要是做一些概念上面的学习.暂时不打算深入实践. 主要参考:http://www.osgi.com.cn/article ...

  10. spring中文乱码问题

    第一:code @RequestMapping(value = "/query/{keyword}", method = RequestMethod.GET, produces = ...