摘要:题目要求即如何从树中选择几个节点,在确保这几个节点不直接相连的情况下使其值的和最大。当前节点的情况有两种选中或是没选中,如果选中的话,那么两个直接子节点将不可以被选中,如果没选中,那么两个直接子节点的状态可以是选中或是没选中。
题目要求
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.
即如何从树中选择几个节点,在确保这几个节点不直接相连的情况下使其值的和最大。
思路和代码最开始的思路我是采用自顶向下递归遍历树的形式来计算可能获得的最大收益。当前节点的情况有两种:选中或是没选中,如果选中的话,那么两个直接子节点将不可以被选中,如果没选中,那么两个直接子节点的状态可以是选中或是没选中。代码如下:
public int rob(TreeNode root) { if(root == null) return 0; int result1 = root.val + (root.left == null ? 0 : rob(root.left.left) + rob(root.left.right)) + (root.right == null ? 0 : rob(root.right.left) + rob(root.right.right)); int result2 = rob(root.left) + rob(root.right); return Math.max(result1, result2); }
这段代码的缺陷在于,我需要遍历这棵树两次,分别为了获取选中当前节点和不选中当前节点的情况。而事实上我们可以在一次遍历中就得到这两个情况对于当前节点的影响,并通过递归将值传递回上一层。很像是一种逆向思维。
public int rob2(TreeNode root) { if (root == null) return 0; int[] value = robSum(root); return Math.max(value[0], value[1]); } private int[] robSum(TreeNode root) { int[] res = new int[2]; if (root == null) return res; int[] left = robSum(root.left); int[] right = robSum(root.right); res[0] = Math.max(left[0], left[1]) + Math.max(right[0], right[1]); res[1] = root.val + left[0] + right[0]; return res; }
想要了解更多开发技术,面试教程以及互联网公司内推,欢迎关注我的微信公众号!将会不定期的发放福利哦~
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/71108.html
摘要:复杂度思路对于每一个位置来说,考虑两种情况分别对和再进行计算。用对已经计算过的进行保留,避免重复计算。 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, calle...
摘要:一番侦察之后,聪明的小偷意识到这个地方的所有房屋的排列类似于一棵二叉树。如果两个直接相连的房子在同一天晚上被打劫,房屋将自动报警。计算在不触动警报的情况下,小偷一晚能够盗取的最高金额。 Description The thief has found himself a new place for his thievery again. There is only one entranc...
摘要:解法真的非常巧妙,不过这道题里仍要注意两个细节。中,为时,返回长度为的空数组建立结果数组时,是包括根节点的情况,是不包含根节点的情况。而非按左右子树来进行划分的。 Problem The thief has found himself a new place for his thievery again. There is only one entrance to this area,...
摘要:你不能连着偷两家因为这样会触发警报系统。现在有一个数组存放着每一家中的可偷金额,问可以偷的最大金额为多少这里考验了动态编程的思想。动态编程要求我们将问题一般化,然后再找到初始情况开始这个由一般到特殊的计算过程。 House Robber I You are a professional robber planning to rob houses along a street. Each...
摘要:注意对边界条件的判断,是否非空,是否长度为 House Robber I Problem You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping y...
阅读 1626·2021-10-25 09:46
阅读 3209·2021-10-08 10:04
阅读 2354·2021-09-06 15:00
阅读 2768·2021-08-19 10:57
阅读 2077·2019-08-30 11:03
阅读 970·2019-08-30 11:00
阅读 2370·2019-08-26 17:10
阅读 3545·2019-08-26 13:36