[抄题]:

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:

Input: [3,2,3,null,3,null,1]

     3
/ \
2 3
\ \
3 1 Output: 7
Explanation: Maximum amount of money the thief can rob = 3 + 3 + 1 = 7.

Example 2:

Input: [3,4,5,1,3,null,1]

     3
/ \
4 5
/ \ \
1 3 1 Output: 9
Explanation: Maximum amount of money the thief can rob = 4 + 5 = 9.

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

[思维问题]:

知道是dc,不知道具体怎么写。用dc可以新生成数组,不需要别的参数。因为数组里面的元素只有2个,指定一下就行了。

[英文数据结构或算法,为什么不用别的数据结构或算法]:

数组:因为只有偷与否2种状态,left right res都需要在其中比较,所以开空间为2的数组即可

[一句话思路]:

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

用dc可以新生成数组,不需要别的参数。因为数组里面的元素只有2个,指定一下就行了。

[复杂度]:Time complexity: O(n) Space complexity: O(n)

[算法思想:迭代/递归/分治/贪心]:

[关键模板化代码]:

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

[是否头一次写此类driver funcion的代码] :

[潜台词] :

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public int rob(TreeNode root) {
//corner case
if (root == null) return 0; //call the robHelper
int[] result = robHelper(root); //compare and return
return Math.max(result[0], result[1]);
} public int[] robHelper(TreeNode root) {
//corner case
if (root == null) return new int[2];
int[] result = new int[2]; //initialization : 2 int[] left and right
int[] left = robHelper(root.left);
int[] right = robHelper(root.right); //define the numbers
//choose root
result[1] = left[0] + root.val + right[0];
//not choose
result[0] = Math.max(left[0], left[1]) + Math.max(right[0], right[1]); //return
return result;
}
}

337. House Robber III二叉树上的抢劫题的更多相关文章

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

  2. Leetcode 337. House Robber III

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

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

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

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

  6. 【LeetCode】337. House Robber III 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  7. Java [Leetcode 337]House Robber III

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

  8. 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 ...

  9. 337 House Robber III 打家劫舍 III

    小偷又发现一个新的可行窃的地点. 这个地区只有一个入口,称为“根”. 除了根部之外,每栋房子有且只有一个父房子. 一番侦察之后,聪明的小偷意识到“这个地方的所有房屋形成了一棵二叉树”. 如果两个直接相 ...

随机推荐

  1. e充电加密破解

    def encrypt(self,stationId ): keys = 'F29E0E39-98E4-F4CC318443' encrypt_obj = pyDes.triple_des(keys, ...

  2. 使外部主机可访问Django服务

    欲让外部主机可访问Django的服务器,需使用如下命令开启服务 python manage.py runserver 0.0.0.0:8000

  3. grep init 与 grep [i]nit

    看grep的知识点的时候,在XXX博客里看到一个这样的例子,一直在纠结,纠结,init与[i]nit 匹配到的东西不应该时一样的嘛,为什么一个匹配得出来,一个不行.后来在群里问了某位大哥,耐心的讲解, ...

  4. centos7 设置时区和时间

    1.设置时区(同步时间前先设置) timedatectl set-timezone Asia/Shanghai 2.安装组件 yum -y install ntp systemctl enable n ...

  5. Python Day5 模块 包

    一:区分Python文件的2种用途 1个Python文件的2种用途 1.1 当作脚本执行:        if __name__ == '__main__': 1.2 当作模块导入使用     if ...

  6. [UE4]Background Blur,背景模糊

    一.Blur Strength:模糊强度 二.背景是模糊的,在Background Blur前面的控件二不会模糊. 三.可以调整顺序,让按钮也模糊.然后按钮被模糊了,但是按钮还可以被点击的,Backg ...

  7. layui流加载+h5自带模板

    @{ ViewBag.Title = "服务列表"; Layout = "~/Areas/hahaha/Views/Shared/_Head.cshtml"; ...

  8. for each in for in 与for of

    for each in for each in是作为E4X标准的一部分在javascript 1.6中发布的,而它不是ECMAScript标准的一部分.  这将意味着存在各种浏览器的兼容性问题.for ...

  9. Python(算法)-时间复杂度和空间复杂度

    时间复杂度 算法的时间复杂度是一个函数,它定量描述了该算法的运行时间,时间复杂度常用“O”表述,使用这种方式时,时间复杂度可被称为是渐近的,它考察当输入值大小趋近无穷时的情况 时间复杂度是用来估计算法 ...

  10. for...in的改进版for...of

    for...in 用起来似乎还不错,为什么又弄个 for...of 呢? 来看个例子: 'user strict' var arr = [12,13,14,15,16]; for(var i in a ...