337-House-Robber-Iii
Sat 17 May 2025
https://leetcode.com/problems/house-robber-iii
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
def rob(self, root: Optional[TreeNode]) -> int:
def robOrNot(root: Optional[TreeNode]) -> tuple:
if not root:
return (0, 0)
robLeft, notRobLeft = robOrNot(root.left)
robRight, notRobRight = robOrNot(root.right)
return (root.val + notRobLeft + notRobRight,
max(robLeft, notRobLeft) + max(robRight, notRobRight))
return max(robOrNot(root))
new Solution().rob()
Score: 5
Category: leetcode