小偷又发现了一个新的可行窃的地区。这个地区只有一个入口,我们称之为 root
。
除了 root
之外,每栋房子有且只有一个“父“房子与之相连。一番侦察之后,聪明的小偷意识到“这个地方的所有房屋的排列类似于一棵二叉树”。 如果 两个直接相连的房子在同一天晚上被打劫 ,房屋将自动报警。
给定二叉树的 root
。返回 在不触动警报的情况下 ,小偷能够盗取的最高金额 。
示例 1:
输入: root = [3,2,3,null,3,null,1] 输出: 7 解释: 小偷一晚能够盗取的最高金额 3 + 3 + 1 = 7
示例 2:
输入: root = [3,4,5,1,3,null,1] 输出: 9 解释: 小偷一晚能够盗取的最高金额 4 + 5 = 9
提示:
- 树的节点数在
[1, 104]
范围内 0 <= Node.val <= 104
记忆化搜索。
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def rob(self, root: TreeNode) -> int:
@cache
def dfs(root):
if root is None:
return 0
if root.left is None and root.right is None:
return root.val
a = dfs(root.left) + dfs(root.right)
b = root.val
if root.left:
b += dfs(root.left.left) + dfs(root.left.right)
if root.right:
b += dfs(root.right.left) + dfs(root.right.right)
return max(a, b)
return dfs(root)
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
private Map<TreeNode, Integer> memo;
public int rob(TreeNode root) {
memo = new HashMap<>();
return dfs(root);
}
private int dfs(TreeNode root) {
if (root == null) {
return 0;
}
if (memo.containsKey(root)) {
return memo.get(root);
}
int a = dfs(root.left) + dfs(root.right);
int b = root.val;
if (root.left != null) {
b += dfs(root.left.left) + dfs(root.left.right);
}
if (root.right != null) {
b += dfs(root.right.left) + dfs(root.right.right);
}
int res = Math.max(a, b);
memo.put(root, res);
return res;
}
}
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
unordered_map<TreeNode*, int> memo;
int rob(TreeNode* root) {
return dfs(root);
}
int dfs(TreeNode* root) {
if (!root) return 0;
if (memo.count(root)) return memo[root];
int a = dfs(root->left) + dfs(root->right);
int b = root->val;
if (root->left) b += dfs(root->left->left) + dfs(root->left->right);
if (root->right) b += dfs(root->right->left) + dfs(root->right->right);
int res = max(a, b);
memo[root] = res;
return res;
}
};
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func rob(root *TreeNode) int {
memo := make(map[*TreeNode]int)
var dfs func(root *TreeNode) int
dfs = func(root *TreeNode) int {
if root == nil {
return 0
}
if _, ok := memo[root]; ok {
return memo[root]
}
a := dfs(root.Left) + dfs(root.Right)
b := root.Val
if root.Left != nil {
b += dfs(root.Left.Left) + dfs(root.Left.Right)
}
if root.Right != nil {
b += dfs(root.Right.Left) + dfs(root.Right.Right)
}
res := max(a, b)
memo[root] = res
return res
}
return dfs(root)
}
func max(a, b int) int {
if a > b {
return a
}
return b
}