meno = {} ifnot root: return0 if root in meno: return meno[root]
a = root.val if root.left: a += self.rob(root.left.left) + self.rob(root.left.right) if root.right: a += self.rob(root.right.left) + self.rob(root.right.right)
b = 0 b += self.rob(root.left) + self.rob(root.right)
defdfs(root): ifnot root: return0 if root in meno: return meno[root]
a = root.val if root.left: a += dfs(root.left.left) + dfs(root.left.right) if root.right: a += dfs(root.right.left) + dfs(root.right.right) b = 0 b += dfs(root.left) + dfs(root.right)
res = max(a, b) meno[root] = res return res return dfs(root)