您的位置:首页 > 其它

LeetCode 337

2016-05-20 21:49 197 查看
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  = 0, leftMaxWithoutRoot  = 0;
int rightMaxWithRoot = 0, rightMaxWithoutRoot = 0;

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 = 0;
int maxWithoutRoot = 0;

traverse( root, &maxWithRoot, &maxWithoutRoot );

return MAX(maxWithRoot, maxWithoutRoot);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: