Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # 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:
- #function returns a tuple (e1, e2) where e1 is amt robbed including the subtree's root while e2 is amt without subtree's root
- def help(root):
- if not root:
- return (0, 0)
- #postorder
- a,b = help(root.left)
- c, d = help(root.right)
- return(b+d+root.val,max(a, b)+max(c,d))
- return max(help(root))
Add Comment
Please, Sign In to add comment