# 0337. 打家劫舍 III ## 题目地址(337. 打家劫舍 III) <https://leetcode-cn.com/problems/house-robber-iii/> ## 题目描述 ``` <pre class="calibre18">``` 在上次打劫完一条街道之后和一圈房屋后,小偷又发现了一个新的可行窃的地区。这个地区只有一个入口,我们称之为“根”。 除了“根”之外,每栋房子有且只有一个“父“房子与之相连。一番侦察之后,聪明的小偷意识到“这个地方的所有房屋的排列类似于一棵二叉树”。 如果两个直接相连的房子在同一天晚上被打劫,房屋将自动报警。 计算在不触动警报的情况下,小偷一晚能够盗取的最高金额。 示例 1: 输入: [3,2,3,null,3,null,1] 3 / \ 2 3 \ \ 3 1 输出: 7 解释: 小偷一晚能够盗取的最高金额 = 3 + 3 + 1 = 7. 示例 2: 输入: [3,4,5,1,3,null,1] 3 / \ 4 5 / \ \ 1 3 1 输出: 9 解释: 小偷一晚能够盗取的最高金额 = 4 + 5 = 9. ``` ``` ## 前置知识 - 二叉树 - 动态规划 ## 公司 - 阿里 - 腾讯 - 百度 - 字节 ## 思路 和 198.house-robber 类似,这道题也是相同的思路。 只不过数据结构从数组换成了树。 我们仍然是对每一项进行决策:**如果我抢的话,所得到的最大价值是多少。如果我不抢的话,所得到的最大价值是多少。** - 遍历二叉树,都每一个节点我们都需要判断抢还是不抢。 - 如果抢了的话, 那么我们不能继续抢其左右子节点 - 如果不抢的话,那么我们可以继续抢左右子节点,当然也可以不抢。抢不抢取决于哪个价值更大。 - 抢不抢取决于哪个价值更大。 这是一个明显的递归问题,我们使用递归来解决。由于没有重复子问题,因此没有必要 cache ,也没有必要动态规划。 ## 关键点 - 对每一个节点都分析,是抢还是不抢 ## 代码 语言支持:JS, C++,Java,Python JavaScript Code: ``` <pre class="calibre18">``` <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">helper</span>(<span class="hljs-params">root</span>) </span>{ <span class="hljs-keyword">if</span> (root === <span class="hljs-params">null</span>) <span class="hljs-keyword">return</span> [<span class="hljs-params">0</span>, <span class="hljs-params">0</span>]; <span class="hljs-title">// 0: rob 1: notRob</span> <span class="hljs-keyword">const</span> l = helper(root.left); <span class="hljs-keyword">const</span> r = helper(root.right); <span class="hljs-keyword">const</span> robed = root.val + l[<span class="hljs-params">1</span>] + r[<span class="hljs-params">1</span>]; <span class="hljs-keyword">const</span> notRobed = <span class="hljs-params">Math</span>.max(l[<span class="hljs-params">0</span>], l[<span class="hljs-params">1</span>]) + <span class="hljs-params">Math</span>.max(r[<span class="hljs-params">0</span>], r[<span class="hljs-params">1</span>]); <span class="hljs-keyword">return</span> [robed, notRobed]; } <span class="hljs-title">/** * @param {TreeNode} root * @return {number} */</span> <span class="hljs-keyword">var</span> rob = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">root</span>) </span>{ <span class="hljs-keyword">const</span> [robed, notRobed] = helper(root); <span class="hljs-keyword">return</span> <span class="hljs-params">Math</span>.max(robed, notRobed); }; ``` ``` C++ Code: ``` <pre class="calibre18">``` <span class="hljs-title">/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */</span> <span class="hljs-keyword">class</span> Solution { <span class="hljs-keyword">public</span>: <span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">rob</span><span class="hljs-params">(TreeNode* root)</span> </span>{ pair<<span class="hljs-keyword">int</span>, <span class="hljs-keyword">int</span>> res = dfs(root); <span class="hljs-keyword">return</span> max(res.first, res.second); } pair<<span class="hljs-keyword">int</span>, <span class="hljs-keyword">int</span>> dfs(TreeNode* root) { pair<<span class="hljs-keyword">int</span>, <span class="hljs-keyword">int</span>> res = {<span class="hljs-params">0</span>, <span class="hljs-params">0</span>}; <span class="hljs-keyword">if</span>(root == <span class="hljs-params">NULL</span>) { <span class="hljs-keyword">return</span> res; } pair<<span class="hljs-keyword">int</span>, <span class="hljs-keyword">int</span>> left = dfs(root->left); pair<<span class="hljs-keyword">int</span>, <span class="hljs-keyword">int</span>> right = dfs(root->right); <span class="hljs-title">// 0 代表不偷,1 代表偷</span> res.first = max(left.first, left.second) + max(right.first, right.second); res.second = left.first + right.first + root->val; <span class="hljs-keyword">return</span> res; } }; ``` ``` Java Code: ``` <pre class="calibre18">``` <span class="hljs-title">/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Solution</span> </span>{ <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">int</span> <span class="hljs-title">rob</span><span class="hljs-params">(TreeNode root)</span> </span>{ <span class="hljs-keyword">int</span>[] res = dfs(root); <span class="hljs-keyword">return</span> Math.max(res[<span class="hljs-params">0</span>], res[<span class="hljs-params">1</span>]); } <span class="hljs-keyword">public</span> <span class="hljs-keyword">int</span>[] dp(TreeNode root) { <span class="hljs-keyword">int</span>[] res = <span class="hljs-keyword">new</span> <span class="hljs-keyword">int</span>[<span class="hljs-params">2</span>]; <span class="hljs-keyword">if</span>(root == <span class="hljs-keyword">null</span>) { <span class="hljs-keyword">return</span> res; } <span class="hljs-keyword">int</span>[] left = dfs(root.left); <span class="hljs-keyword">int</span>[] right = dfs(root.right); <span class="hljs-title">// 0 代表不偷,1 代表偷</span> res[<span class="hljs-params">0</span>] = Math.max(left[<span class="hljs-params">0</span>], left[<span class="hljs-params">1</span>]) + Math.max(right[<span class="hljs-params">0</span>], right[<span class="hljs-params">1</span>]); res[<span class="hljs-params">1</span>] = left[<span class="hljs-params">0</span>] + right[<span class="hljs-params">0</span>] + root.val; <span class="hljs-keyword">return</span> res; } } ``` ``` Python Code: ``` <pre class="calibre18">``` <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Solution</span>:</span> <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">rob</span><span class="hljs-params">(self, root: TreeNode)</span> -> int:</span> <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">dfs</span><span class="hljs-params">(node)</span>:</span> <span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> node: <span class="hljs-keyword">return</span> [<span class="hljs-params">0</span>, <span class="hljs-params">0</span>] [l_rob, l_not_rob] = dfs(node.left) [r_rob, r_not_rob] = dfs(node.right) <span class="hljs-keyword">return</span> [node.val + l_not_rob + r_not_rob, max([l_rob, l_not_rob]) + max([r_rob, r_not_rob])] <span class="hljs-keyword">return</span> max(dfs(root)) <span class="hljs-title"># @lc code=end</span> ``` ``` **复杂度分析** - 时间复杂度:O(N)O(N)O(N),其中 N 为树的节点个数。 - 空间复杂度:O(h)O(h)O(h),其中 h 为树的高度。 ## 相关题目 - [198.house-robber](https://github.com/azl397985856/leetcode/blob/master/problems/198.house-robber.md) 大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:<https://github.com/azl397985856/leetcode> 。 目前已经 37K star 啦。 大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。 ![](https://img.kancloud.cn/a3/63/a363818092b0356fbd67882f0389528b_900x500.jpg)