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: [email protected]
 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 }
时间: 2024-12-06 19:42:36

LeetCode 337的相关文章

[LeetCode] 337. 打家劫舍 III (树形dp)

题目 在上次打劫完一条街道之后和一圈房屋后,小偷又发现了一个新的可行窃的地区.这个地区只有一个入口,我们称之为"根". 除了"根"之外,每栋房子有且只有一个"父"房子与之相连.一番侦察之后,聪明的小偷意识到"这个地方的所有房屋的排列类似于一棵二叉树". 如果两个直接相连的房子在同一天晚上被打劫,房屋将自动报警. 计算在不触动警报的情况下,小偷一晚能够盗取的最高金额. 示例 1: 输入: [3,2,3,null,3,null,1

[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 hou

【LeetCode 337 &amp; 329. memorization DFS】House Robber III

/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: //递归程序就是结构演绎,有点像dp,只要定义好每一次递归过程完成的是同一个目标,就能保证所有递归结束之后

Leetcode 337. 打家劫舍 III

/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: struct returnData { int qu; int buqu; returnData(int

LeetCode 337. House Robber III 动态演示

每个节点是个房间,数值代表钱.小偷偷里面的钱,不能偷连续的房间,至少要隔一个.问最多能偷多少钱 TreeNode* cur mp[{cur, true}]表示以cur为根的树,最多能偷的钱 mp[{cur, false}]表示以cur为根的树,不偷cur节点的钱,最多能偷的钱 可以看出有下面的关系 mp[{node, false}] = mp[{node->left,true}] + mp[{node->right,true}] mp[{node, true}] = max(node->

[LeetCode] 337. 打家劫舍III ☆☆☆(动态规划)

https://leetcode-cn.com/problems/house-robber-iii/solution/tong-yong-si-lu-tuan-mie-da-jia-jie-she-wen-ti-b-2/ 描述 在上次打劫完一条街道之后和一圈房屋后,小偷又发现了一个新的可行窃的地区.这个地区只有一个入口,我们称之为“根”. 除了“根”之外,每栋房子有且只有一个“父“房子与之相连.一番侦察之后,聪明的小偷意识到“这个地方的所有房屋的排列类似于一棵二叉树”. 如果两个直接相连的房子在

LeetCode编程总结

# 1.在有序表中查找两数组指定的和,双指针法# 2.滑动窗口 : 连续子数组之和# 3.二分查找 : 顺序数组中查找特定的值 # 4.递归程序的真正的构建是从底向上的,这就是为什么递归终止条件要写在最前面# 参见 反转链表的递归程序 LeetCode206 # 5. 链表归并排序的递归过程,要好好体会一下 # 6.树型的暴力搜索(全路径搜索 .DFS )需要进行回溯 参见 LeetCode 93 # 7.利用 return 能使树型递归提早结束,类似于DFS 参见 LeetCode 37 #

[总结]树

树作为一种基本的数据结构,也是算法题常考的题型.基本的如树的遍历,树的高度,树的变种数据结构等. 树的遍历 树的遍历有四种:前序,中序,后序,层次.都需要掌握其递归与非递归方式. [leetcode]94.Binary Tree Inorder Traversal 中序遍历 # Definition for a binary tree node. # class TreeNode: # def __init__(self, x): # self.val = x # self.left = Non

&amp;lt;LeetCode OJ&amp;gt; 337. House Robber III

Total Accepted: 1341 Total Submissions: 3744 Difficulty: Medium 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