House Robber III LeetCode solution in Python
1class Solution:2def rob(self, root: Optional[TreeNode]) -> int:3hashMap = {}45def helper(root: Optional[TreeNode]) -> int:6if not root:7return 08if root in hashMap:9return hashMap[root]10ansOption1 = root.val11if root.left is not None:12ansOption1 += helper(root.left.left) + helper(root.left.right)13if root.right is not None:14ansOption1 += helper(root.right.left) + helper(root.right.right)15ansOption2 = helper(root.left) + helper(root.right)16ansFinal = max(ansOption1, ansOption2)17hashMap[root] = ansFinal18return ansFinal1920return helper(root)