0%
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
class Solution: def rob(self, root: Optional[TreeNode]) -> int: def robtree(root)->List[int]: if root==None:return [0,0] left_arr = robtree(root.left) right_arr = robtree(root.right) rob = root.val + left_arr[0] + right_arr[0] norob = max(left_arr[0],left_arr[1]) + max(right_arr[0],right_arr[1]) return [ norob, rob ] result = robtree(root) return max(result[0],result[1])
|