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.

 /*************************************************************************
> File Name: LeetCode337.c
> Author: Juntaran
> Mail: JuntaranMail@gmail.com
> Created Time: Wed 11 May 2016 19:08:25 PM CST
************************************************************************/ /************************************************************************* 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. ************************************************************************/ #include <stdio.h> /*
继198与213
*/ /*
Discuss区大神答案
https://leetcode.com/discuss/91777/intuitive-still-efficient-solution-accepted-well-explained
另外有C++详解
https://leetcode.com/discuss/91899/step-by-step-tackling-of-the-problem
*/ /**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/ #define MAX(a, b) ((a) > (b) ? (a) : (b)) // struct TreeNode
// {
// int val;
// struct TreeNode *left;
// struct TreeNode *right;
// }; void traverse( struct TreeNode* root, int* maxWithRoot, int* maxWithoutRoot )
{
int leftMaxWithRoot = , leftMaxWithoutRoot = ;
int rightMaxWithRoot = , rightMaxWithoutRoot = ; if( root )
{
traverse( root->left, &leftMaxWithRoot, &leftMaxWithoutRoot );
traverse( root->right, &rightMaxWithRoot, &rightMaxWithoutRoot ); *maxWithRoot = leftMaxWithoutRoot + rightMaxWithoutRoot + root->val;
*maxWithoutRoot = MAX( leftMaxWithRoot, leftMaxWithoutRoot ) + MAX( rightMaxWithRoot, rightMaxWithoutRoot );
}
} int rob(struct TreeNode* root)
{
int maxWithRoot = ;
int maxWithoutRoot = ; traverse( root, &maxWithRoot, &maxWithoutRoot ); return MAX(maxWithRoot, maxWithoutRoot);
}

LeetCode 337的更多相关文章

  1. Leetcode 337. House Robber III

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

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

  3. Leetcode 337. 打家劫舍 III

    题目链接 https://leetcode.com/problems/house-robber-iii/description/ 题目描述 在上次打劫完一条街道之后和一圈房屋后,小偷又发现了一个新的可 ...

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

  5. Java实现 LeetCode 337 打家劫舍 III(三)

    337. 打家劫舍 III 在上次打劫完一条街道之后和一圈房屋后,小偷又发现了一个新的可行窃的地区.这个地区只有一个入口,我们称之为"根". 除了"根"之外,每 ...

  6. [LeetCode] 337. 打家劫舍 III (树形dp)

    题目 在上次打劫完一条街道之后和一圈房屋后,小偷又发现了一个新的可行窃的地区.这个地区只有一个入口,我们称之为"根". 除了"根"之外,每栋房子有且只有一个&q ...

  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 337.大家结舍III

    打家劫舍III 在上次打劫完一条街道之后和一圈房屋后,小偷又发现了一个新的可行窃的地区.这个地区只有一个入口,我们称之为"根".除了"根"之外,每栋房子有且只有 ...

  9. 【LeetCode 337 & 329. memorization DFS】House Robber III

    /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...

随机推荐

  1. C# —— IList, ArrayList与List的区别详解

    共同点:    IList, List , ArrayList 通俗一点来讲就是广义的数组,C#里面称之为集合.不同于一般的狭义的数组,它们可以存放任意类型的东西,在申明或者赋值的时候指定. 比如你写 ...

  2. (剑指Offer)面试题30:最小的k个数

    题目: 输入n个整数,找出其中最小的K个数.例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4,. 思路: 1.排序 把输入的n个整数排序,然后取前k个数: 时间复杂度 ...

  3. JS瀑布流布局模式(2)

    这个例子与上一篇类似,唯一的区别是排序的方式有差别.上一篇是在高度最小的列里插入内容,这个案例是按顺序放置内容. 两种方法各有优缺点.第一种需要在图片内容加载完成的情况下有效,各个列的图高度差异不大. ...

  4. 汽车常用的ECU芯片

    Power Train ECU的CPU用的比较多的基本来自于Infineon,ST,Freescale BOSCH的16位ECU M(E)7系列主要使用C167内核的CPU,早期的M(E)7系列使用西 ...

  5. hdu 5264 pog loves szh I 水题

    pog loves szh I Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?p ...

  6. C#中使用SQLite数据库简介(上)

    [SQLite数据库] SQLite是一个开源的轻量级的桌面型数据库,它将几乎所有数据库要素(包括定义.表.索引和数据本身)都保存在一个单一的文件中.SQLite用C编写实现,它在内存消耗.文件体积. ...

  7. Android程序开发0基础教程(一)

    程序猿学英语就上视觉英语网 Android程序开发0基础教程(一)   平台简单介绍   令人激动的Google手机操作系统平台-Android在2007年11月13日正式公布了,这是一个开放源码的操 ...

  8. android获取/更改gps和WIFI状态

    一.WIFI状态的获取和更改 适用于 SDK1.0 , SDK1.5 1.获取WIFI状态 方法1:通过WifiManager进行操作 1WifiManager wifiManager = (Wifi ...

  9. [Angular2 Form] Group Inputs in Angular 2 Forms with ngModelGroup

    The ngModelGroup directive allows you to group together related inputs so that you structure the obj ...

  10. boa安装

    Boa 下载地址:http://www.boa.org/boa-0.94.13.tar.gz 1.解压生成Makefile tar xzf boa-0.94.13.tar.gz #解压 cd boa- ...