337. House Robber III——树的题目几乎都是BFS、DFS,要么递归要么循环
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,要么递归要么循环的更多相关文章
- Leetcode 337. House Robber III
337. House Robber III Total Accepted: 18475 Total Submissions: 47725 Difficulty: Medium The thief ha ...
- 337. House Robber III(包含I和II)
198. House Robber You are a professional robber planning to rob houses along a street. Each house ha ...
- 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:二叉树下的不能相邻,求能 ...
- [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 ...
- Java [Leetcode 337]House Robber III
题目描述: The thief has found himself a new place for his thievery again. There is only one entrance to ...
- [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 ...
- 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 ...
- 337. House Robber III二叉树上的抢劫题
[抄题]: The thief has found himself a new place for his thievery again. There is only one entrance to ...
- LeetCode 337. House Robber III 动态演示
每个节点是个房间,数值代表钱.小偷偷里面的钱,不能偷连续的房间,至少要隔一个.问最多能偷多少钱 TreeNode* cur mp[{cur, true}]表示以cur为根的树,最多能偷的钱 mp[{c ...
随机推荐
- CUBRID学习笔记 19 sql语句1
创建 欢迎转载 ,转载时请保留作者信息.本文版权归本人所有,如有任何问题,请与我联系wang2650@sohu.com . 过错 create table tableName (字段名 字段类型 pr ...
- Android——android学习(android目录与AndroidManifest解析)
res目录:存放android项目的各种资源文件 layout:存放界面布局文件 values:存放各种xml格式的资源文件 strings.xml:字符串资源文件: colors.xml:颜色资源文 ...
- python_way ,day1 编译安装python3、基础及流程控制
本节内容: 1,Python介绍发展史 2,安装 3,Hello World 4,程序 5,变量,字符编码 6,用户输入 7,模块初识 一.python介绍 python的创始人为吉多·范罗苏姆(Gu ...
- Python基础学习笔记(六)常用列表操作函数和方法
参考资料: 1. <Python基础教程> 2. http://www.runoob.com/python/python-lists.html 3. http://www.liaoxuef ...
- luabind 导出string问题
luabind导出字符串 不能导出char* 会有问题 应该是字符串连接的时候出错了 static _TCHAR* pRetChar = new _TCHAR[10]; memcpy(pRetChar ...
- BUAA & ICT 夏令营之旅
我还只有二十几岁,总应该相信点什么吧. ================================ 7.10午后坐火车赶到北京.一路上火车行驶在茫茫云海里.车窗外的世界是这样子的:一片广袤的原野 ...
- gcc常用指令及相关知识
1,gcc与g++的问题: 1.后缀为.c的,gcc把它当作是C程序,而g++当作是c++程序:后缀为.cpp的,两者都会认为是c++程序. 2.编译阶段,g++会调用gcc,对于c++代码,两者是等 ...
- html5实现GIF动画!
代码如下: <!DOCTYPE html><html> <head> <meta charset="utf-8"&g ...
- 使用RestTemplate Spring安全认证
使用RestTemplate Spring安全认证 java spring 认证authentication 安全spring-security 我有提供2个独立的一整套服务2 Spring的web应 ...
- SDL2.0的几何图行绘画
SDL2.0的几何图形绘画 通过SDL_Window.SDL_Renderer.SDL_Texture三者实现了简单的几何图形绘画. 包括了SDL_RenderDrawPoint.SDL_Render ...