Complex Numbers
Solution:\n\n    def rob(self, root):\n        def rob(root):\n            if not root:\n                return 0\n            z = root.val * 1j + rob(root.left
**Solution:**\n\n    def rob(self, root):\n        def rob(root):\n            if not root:\n                return 0\n            z = root.val * 1j + rob(root.left) + rob(root.right)\n            return max(z.real, z.imag) + z.real * 1j\n        return int(rob(root).real)\n\nThe helper function returns a complex number. The real part tells, well, the real answer (maximum amount with or without robbing the (sub)tree's root). The imaginary part tells the maximum amount without robbing the root. (But in the temporary variable `z`, the real part is the amount without the root and the imaginary part is the amount with the root.)\n\n---\n\n**Shorter version:**\n\n    def rob(self, root):\n        def rob(root):\n            z = root.val * 1j + rob(root.left) + rob(root.right) if root else 0\n            return max(z.real, z.imag) + z.real * 1j\n        return int(rob(root).real)\n\n---\n\n**Tuple version:**\n\nHere the helper returns a tuple, first element being the real answer and second element being the without-root answer. Variables "y/n" mean yes/no, telling whether the subtree's root might be robbed.\n\n    def rob(self, root):\n        def rob(root):\n            if not root:\n                return 0, 0\n            y, n = rob(root.left)\n            Y, N = rob(root.right)\n            return max(root.val + n + N, y + Y), y + Y\n        return rob(root)[0]\n\nA variation:\n\n    def rob(self, root):\n        def rob(root):\n            if not root:\n                return 0, 0\n            y, n = map(sum, zip(rob(root.left), rob(root.right)))\n            return max(root.val + n, y), y\n        return rob(root)[0]\n\nAnother:\n\n    def rob(self, root):\n        def rob(root):\n            y, n = map(sum, zip(rob(root.left), rob(root.right), (0, root.val))) if root else (0, 0)\n            return max(y, n), y\n        return rob(root)[0]
['Python']