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 }
  • 相关阅读:
    solr一些错误
    maven聚合工程之找不到符号,类等错误
    图片服务器搭建及上传测试,网络配置问题(errno: 111)
    安装Nginx,make时报错:/usr/bin/ld: cannot find -lfdfsclient或者类似找不到-lxxx
    关于PCA算法的学习
    生物信息及开发常用的Linux命令
    05.课程主页面三个接口开发
    04.增加media文件配置
    3.课程相关5张表设计
    2.增加抽象基类
  • 原文地址:https://www.cnblogs.com/Juntaran/p/5513528.html
Copyright © 2011-2022 走看看