337. House Robber III
https://leetcode.com/problems/house-robber-iii/
Python
DFS (Time Limit Exceed)
class Solution:
def rob(self, root: Optional[TreeNode]) -> int:
def dfs(node, can_pick: bool):
if not node:
return 0
skip = dfs(node.left, True) + dfs(node.right, True)
if not can_pick:
return skip
pick = node.val + dfs(node.left, False) + dfs(node.right, False)
return max(pick, skip)
return max(dfs(root, False), dfs(root, True))