DP solution Python
\nfrom collections import defaultdict\n\nclass Solution:\n    def rob(self, root: Optional[TreeNode]) -> int:\n        def rec(node, depth):\n            if not
```\nfrom collections import defaultdict\n\nclass Solution:\n    def rob(self, root: Optional[TreeNode]) -> int:\n        def rec(node, depth):\n            if not node:\n                return\n            layer_dict[depth].append(node)\n            rec(node.left, depth + 1)\n            rec(node.right, depth + 1)\n            \n        #get layer dict\n        layer_dict = defaultdict(list)\n        rec(root, 0)\n        \n        #dp\n        dp_use, dp_no_use = {None : 0}, {None : 0}\n        \n        for layer in range(max(layer_dict.keys()), -1, -1):\n            for u in layer_dict[layer]:\n                dp_use[u] = dp_no_use[u.left] + dp_no_use[u.right] + u.val\n                dp_no_use[u] = max(dp_use[u.left], dp_no_use[u.left]) + max(dp_use[u.right], dp_no_use[u.right])\n        \n        return max(dp_use[root], dp_no_use[root])\n```
['Dynamic Programming', 'Python', 'Python3']