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.git
Determine the maximum amount of money the thief can rob tonight without alerting the police.github
Example 1:数组
Input: [3,2,3,null,3,null,1] 3 / \ 2 3 \ \ 3 1 Output: 7 Explanation: Maximum amount of money the thief can rob = 3 + 3 + 1 = 7.
Example 2:ide
Input: [3,4,5,1,3,null,1] 3 / \ 4 5 / \ \ 1 3 1 Output: 9 Explanation: Maximum amount of money the thief can rob = 4 + 5 = 9.
在上次打劫完一条街道以后和一圈房屋后,小偷又发现了一个新的可行窃的地区。这个地区只有一个入口,咱们称之为“根”。 除了“根”以外,每栋房子有且只有一个“父“房子与之相连。一番侦察以后,聪明的小偷意识到“这个地方的全部房屋的排列相似于一棵二叉树”。 若是两个直接相连的房子在同一天晚上被打劫,房屋将自动报警。ui
计算在不触动警报的状况下,小偷一晚可以盗取的最高金额。this
示例 1:code
输入: [3,2,3,null,3,null,1] 3 / \ 2 3 \ \ 3 1 输出: 7 解释: 小偷一晚可以盗取的最高金额 = 3 + 3 + 1 = 7.
示例 2:orm
输入: [3,4,5,1,3,null,1] 3 / \ 4 5 / \ \ 1 3 1 输出: 9 解释: 小偷一晚可以盗取的最高金额 = 4 + 5 = 9.
# -*- coding: utf-8 -*- # @Author: 何睿 # @Create Date: 2019-04-07 10:43:47 # @Last Modified by: 何睿 # @Last Modified time: 2019-04-07 11:00:22 class Solution: def rob(self, root): """ :type root: TreeNode :rtype: int """ return max(self.recursion(root)) def recursion(self, root): # result[0] 表示偷当前的房子,result[1] 表示不偷当前的房子 if not root: return [0, 0] result = [0, 0] left = self.recursion(root.left) right = self.recursion(root.right) # 偷当前的房子:result[0] :当前节点的值+ 左右子树中不偷根节点房子的最大值 result[0] = root.val + left[1] + right[1] # 不偷当前的房子:result[1] 那么左右子树的根节点偷不偷均可以,选最大值 result[1] = max(left[0], left[1]) + max(right[0], right[1]) return result