zoukankan      html  css  js  c++  java
  • LeetCode 337

    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.

     1 /*************************************************************************
     2     > File Name: LeetCode337.c
     3     > Author: Juntaran
     4     > Mail: JuntaranMail@gmail.com
     5     > Created Time: Wed 11 May 2016 19:08:25 PM CST
     6  ************************************************************************/
     7  
     8 /*************************************************************************
     9     
    10     House Robber III
    11     
    12     The thief has found himself a new place for his thievery again. 
    13     There is only one entrance to this area, called the "root." 
    14     Besides the root, each house has one and only one parent house. 
    15     After a tour, the smart thief realized that 
    16     "all houses in this place forms a binary tree". 
    17     It will automatically contact the police if 
    18     two directly-linked houses were broken into on the same night.
    19 
    20     Determine the maximum amount of money the thief can rob tonight 
    21     without alerting the police.
    22 
    23     Example 1:
    24          3
    25         / 
    26        2   3
    27             
    28          3   1
    29     Maximum amount of money the thief can rob = 3 + 3 + 1 = 7.
    30     Example 2:
    31          3
    32         / 
    33        4   5
    34       /     
    35      1   3   1
    36     Maximum amount of money the thief can rob = 4 + 5 = 9.
    37 
    38  ************************************************************************/
    39 
    40 #include <stdio.h>
    41 
    42 /*
    43     继198与213
    44 */
    45 
    46 
    47 /*
    48     Discuss区大神答案
    49     https://leetcode.com/discuss/91777/intuitive-still-efficient-solution-accepted-well-explained
    50     另外有C++详解
    51     https://leetcode.com/discuss/91899/step-by-step-tackling-of-the-problem
    52 */
    53 
    54 /**
    55  * Definition for a binary tree node.
    56  * struct TreeNode {
    57  *     int val;
    58  *     struct TreeNode *left;
    59  *     struct TreeNode *right;
    60  * };
    61  */
    62  
    63 #define MAX(a, b) ((a) > (b) ? (a) : (b))
    64 
    65 // struct TreeNode 
    66 // {
    67     // int val;
    68     // struct TreeNode *left;
    69     // struct TreeNode *right;
    70 // };
    71 
    72 void traverse( struct TreeNode* root, int* maxWithRoot, int* maxWithoutRoot )
    73 {
    74     int leftMaxWithRoot  = 0, leftMaxWithoutRoot  = 0;
    75     int rightMaxWithRoot = 0, rightMaxWithoutRoot = 0;
    76     
    77     if( root )
    78     {
    79         traverse( root->left,  &leftMaxWithRoot,  &leftMaxWithoutRoot  );
    80         traverse( root->right, &rightMaxWithRoot, &rightMaxWithoutRoot );
    81         
    82         *maxWithRoot    = leftMaxWithoutRoot + rightMaxWithoutRoot + root->val;
    83         *maxWithoutRoot = MAX( leftMaxWithRoot, leftMaxWithoutRoot ) + MAX( rightMaxWithRoot, rightMaxWithoutRoot );
    84     }
    85 }
    86 
    87 int rob(struct TreeNode* root) 
    88 {
    89     int maxWithRoot = 0;
    90     int maxWithoutRoot = 0;
    91     
    92     traverse( root, &maxWithRoot, &maxWithoutRoot );
    93     
    94     return MAX(maxWithRoot, maxWithoutRoot);
    95 }
  • 相关阅读:
    CS184.1X 计算机图形学导论 作业0
    计算机导论学习(第0单元)
    计算机图形学(第一讲.)
    计算机图形学(第一讲)
    云计算和大数据时代网络技术揭秘(二)云与网的关系
    云计算和大数据时代网络技术揭秘(一)云计算的兴起
    python3练习100题——017
    python3练习100题——016
    python3练习100题——014
    python3练习100题——015
  • 原文地址:https://www.cnblogs.com/Juntaran/p/5513528.html
Copyright © 2011-2022 走看看