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. OC:属性、点语法、KVC

    //属性的属性 属性定义在一个 .h文件里,在这个.h文件里可以定义实例变量(就是这个类的特征),也可以通过   @protery(属性约束关键字) 属性名字类型 属性名 来定义一些属性,在prope ...

  2. window.print打印指定div

    window.print可以打印网页,但有时候我们只希望打印特定控件或内容,怎么办呢? 首先我们可以把要打印的内容放在div中,然后用下面的代码进行打印. <html> <head& ...

  3. weblogic/utils/NestedException

    Working with Weblogic 8.1, it’s fine just to put jar of weblogic-8.1.jar into your classpath, your c ...

  4. C# 反射 通过类名创建类实例

    “反射”其实就是利用程序集的元数据信息. 反射可以有很多方法,编写程序时请先导入 System.Reflection 命名空间. 1.假设你要反射一个 DLL 中的类,并且没有引用它(即未知的类型): ...

  5. 前端优化分析 之 javascript引用位置优化

    在很多优化法则中都提到,尽量将javascript放到页面底部,这是为什么呢 我通过firebug进行了下简单的分析 看下图  本页面首尾都存在javascript代码 我们分析得出 1.整个页面文档 ...

  6. 为网页设计师和开发者准备的20个很棒的JavaScript资源

    JavaScript是一门应用广泛的计算机编程语言,一般具应用在Web浏览器中,大多用于客户端脚本以实现用户与服务器的交互.在游戏开发.移动应用.一些大型的服务器应用等开发进程中它在服务器端的应用也很 ...

  7. UVA 12897 Decoding Baby Boos 暴力

    Decoding Baby Boos Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contes ...

  8. Fox Roddick interviw Federer before 2013 US Open

    talk about  Mike Jordan , talk about Tiger Woods, their competitor when people discuss you I love wi ...

  9. ProgressCircular

    https://github.com/eltld/ProgressCircular

  10. HTML之一语言代码

    HTML的lang属性可用于网页或部分网页的语言.这对搜索引擎和浏览器是有帮助的. 同时也可以是指HTTP Header中的Accept-Language/Content-Language. ISO ...