原题链接在这里:https://leetcode.com/problems/house-robber-iii/

题目:

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.

题解:

List some examples and find out this has to be done with DFS.

One or null node is easy to think, thus use DFS bottom-up, devide and conquer.

Then it must return value on dfs. Each dfs needs current node and return [robRoot, notRobRoot], which denotes rob or skip current node.

robRoot = notRobLeft + notRobRight + root.val

notRobRoot = Math.max(robLeft, notRobLeft) + Math.max(robRight, notRobRight).

Time Complexity: O(n).

Space: O(logn). 用了logn层stack.

AC Java:

 /**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public int rob(TreeNode root) {
int [] res = dfs(root);
return Math.max(res[0], res[1]);
}
private int [] dfs(TreeNode root){
int [] dp = new int[2];
if(root == null){
return dp;
}
int [] left = dfs(root.left);
int [] right = dfs(root.right);
//dp[0]表示偷root的, 那么左右都不能偷, 所以用left[1], right[1].
dp[0] = left[1] + right[1] + root.val;
//dp[1]表示不偷root的, 那么左右偷不偷都可以, 取最大值
dp[1] = Math.max(left[0], left[1]) + Math.max(right[0], right[1]);
return dp;
}
}

类似House RobberHouse Robber II.

LeetCode House Robber III的更多相关文章

  1. [LeetCode] House Robber III 打家劫舍之三

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

  2. Leetcode 337. House Robber III

    337. House Robber III Total Accepted: 18475 Total Submissions: 47725 Difficulty: Medium The thief 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] House Robber II 打家劫舍之二

    Note: This is an extension of House Robber. After robbing those houses on that street, the thief has ...

  5. [LeetCode] House Robber 打家劫舍

    You are a professional robber planning to rob houses along a street. Each house has a certain amount ...

  6. [LintCode] House Robber III 打家劫舍之三

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

  7. LeetCode House Robber

    原题链接在这里:https://leetcode.com/problems/house-robber/ 题目: You are a professional robber planning to ro ...

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

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

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

随机推荐

  1. python_配置

    代码示例:https://pan.baidu.com/s/1pLjLPSv 1.自动补全功能 许多人都知道 iPython 有很好的自动补全能力,但是就未必知道 python 也同样可以 Tab 键补 ...

  2. Linux与Windows xp操作系统启动过程

    Linux启动过程: 第一步,加载BIOS,当你打开计算机电源,计算机会首先加载BIOS信息,BIOS信息是如此的重要,以至于计算机必须在最开始就找到它.这是因为BIOS中包含了CPU的相关信息.设备 ...

  3. intellij idea使用

    第02章 IntelliJ IDEA起步 熟悉IntelliJ IDEA用户界面 05 状态条 如何使用intellij idea 从Eclipse转移到IntelliJ IDEA一点心得 Intel ...

  4. 打造自己的php动态连接库文件

    http://blog.163.com/weibin_li/blog/static/1901464172012325115517181/

  5. OneThink开发框架

    OneThink是一个开源的内容管理框架,基于最新的ThinkPHP3.2版本开发,提供更方便.更安全的WEB应用开发体验,采用了全新的架构设计和命名空间机制,融合了模块化.驱动化和插件化的设计理念于 ...

  6. 读取EXCEL数据到内存DataTable

    protected void Page_Load(object sender, EventArgs e) { string filepath = Server.MapPath("~/file ...

  7. 全文检索原理以及es

    最近要做个文章搜索,对全文检索原理以及es原理进行了一些调研, 1.  es索引文件为多个文本文件描述,索引文件中的内容构成可见 http://elasticsearch.cn/article/86 ...

  8. Socket 类通信例子-第24章

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  9. 首师大附中互测题:LJX的校园:入学典礼【C003】

    [C003]LJX的校园:入学典礼[难度C]—————————————————————————————————————————————————————————————————————————————— ...

  10. Go语言练习:go语言与C语言的交互——cgo

    1.代码 package main import "fmt" /* #include <stdlib.h> #include <stdio.h> void ...