Short Python solution

  • 2
    class Solution(object):
        def rob(self, root):
            def solve(root):
                if not root:
                    return 0, 0
                left, right = solve(root.left), solve(root.right)
                return (root.val + left[1] + right[1]), (max(left) + max(right))
            return max(solve(root))

  • 0

    Brilliant idea! I think using cache can further speed it up.

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.