标签:
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 }
标签:
原文地址:http://www.cnblogs.com/Juntaran/p/5513528.html