树的dp问题
# 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: Optional[TreeNode]) -> int:
res = self.rob_tree(root)
return max(res)
def rob_tree(self,node):
if node is None:
return (0,0)
left = self.rob_tree(node.left)
right = self.rob_tree(node.right)
val1 = node.val + left[1] + right[1]
val2 = max(left[0],left[1]) + max(right[0],right[1])
return [val1,val2]