1. Description

  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.

2. Answer

/**
* 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) {
if (root == null)
return 0;
int first = root.val;
int second = 0;
if (root.left != null) {
first += rob(root.left.left);
first += rob(root.left.right);
second += rob(root.left);
}
if (root.right != null) {
first += rob(root.right.left);
first += rob(root.right.right);
second += rob(root.right);
} return (first > second) ? first : second;
}
}

【LeetCode】House Robber III(337)的更多相关文章

  1. 【leetcode】Combination Sum III(middle)

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  2. 【LeetCode】数组--合并区间(56)

    写在前面   老粉丝可能知道现阶段的LeetCode刷题将按照某一个特定的专题进行,之前的[贪心算法]已经结束,虽然只有三个题却包含了简单,中等,困难这三个维度,今天介绍的是第二个专题[数组] 数组( ...

  3. 【leetcode】Number of Islands(middle)

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

  4. 【LeetCode】数组排列问题(permutations)(附加next_permutation解析)

    描述 Given a collection of distinct integers, return all possible permutations. Example: Input: [1,2,3 ...

  5. 【LeetCode】Increasing Triplet Subsequence(334)

    1. Description Given an unsorted array return whether an increasing subsequence of length 3 exists o ...

  6. 【leetcode】Regular Expression Matching (hard) ★

    Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...

  7. 【leetcode】Insertion Sort List (middle)

    Sort a linked list using insertion sort. 思路: 用插入排序对链表排序.插入排序是指每次在一个排好序的链表中插入一个新的值. 注意:把排好序的部分和未排序的部分 ...

  8. 【leetcode】Reverse Linked List(easy)

    Reverse a singly linked list. 思路:没啥好说的.秒... ListNode* reverseList(ListNode* head) { ListNode * rList ...

  9. 【leetcode】Repeated DNA Sequences(middle)★

    All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...

随机推荐

  1. Unity3D UGUI之fbx模型导入问题

    在UI层添加了一个fbx的模型,但是在game模式和运行中无法看到这个fbx. Canvas上面有个Render Mode  :Screen Space - Overlay.Screen Space ...

  2. Eclipse更新SDK速度慢,解决办法

    在SDK Manager -> tools -> options中: HTTP Proxy Server: mirrors.neusoft.edu.cn HTTP Proxy Port: ...

  3. java学习之面向对象(2)

    在描述事物的时候,该事物已存在就具备的一些内容,这是我们就可以把它们都定义在构造函数中,那么什么是构造函数呢? 构造函数就是构建创造对象时调用的函数,它可以给对象进行初始化.构造函数与类名相同,第一个 ...

  4. 整理一自己不怎么熟悉的HTML标签(会陆续更新)

    ---恢复内容开始--- 小白刚开始接触HTML和CSS,在学习过程中发现又遇到很多不认识的标签,于是就想把他们都记录下来,一来可加深记忆,二来也方便以后查阅,当然如果能帮助到你们也是很开心的啦! 1 ...

  5. win7 64位下 mongodb安装及命令运行

    有网友老催我把框架加上mongodb的支持,于是偶尔抽空看了看相关的文章. 今天有缘,就把mongodb安装了一下,中间遇到了小小的问题,So,把整个过程记录一下: 1:先上官网:http://www ...

  6. Entity Framework 基础知识走马观花

    本文目录: 一.EF中的edmx文件探秘 二.EF中的代理模式探秘 三.EF中的延迟加载与即时加载 一.EF中的edmx文件 1.1 emdx文件本质:一个XML文件 (1)通过选择以XML方式打开e ...

  7. 如何在 ASP.NET MVC 中集成 AngularJS(2)

    在如何在 ASP.NET MVC 中集成 AngularJS(1)中,我们介绍了 ASP.NET MVC 捆绑和压缩.应用程序版本自动刷新和工程构建等内容. 下面介绍如何在 ASP.NET MVC 中 ...

  8. RAID 概述

      原创地址:http://www.cnblogs.com/jfzhu/p/3999283.html 转载请注明出处   独立硬盘冗余阵列(RAID, Redundant Array of Indep ...

  9. Cookie与Session

    再说Cookie与Session之前,先要了解一下http协议. 何为http协议: http协议即超文本传输协议,一种基于浏览器请求与服务器响应的协议,该协议主要的特点就是它是一种无状态的协议(只针 ...

  10. Linux内核目录结构

    arch 包括所有和体系结构相关的核心代码. include 包括编译内核所需要的大部分头文件 init 包含内核的初始化代码(不是系统的引导代码),有main.c和Version.c两个文件 mm ...