-
Notifications
You must be signed in to change notification settings - Fork 1
/
house-robber-iii.py
30 lines (24 loc) · 970 Bytes
/
house-robber-iii.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
from typing import Optional
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
class Solution:
# memoized dfs
# TC: O(n) where n is the number of nodes in the tree
# SC: O(n)
def rob(self, root: Optional[TreeNode]) -> int:
def dfs(node, taken, mem):
if node == None: return 0
if (node, taken) in mem:
return mem[(node, taken)]
if taken:
mem[(node, taken)] = dfs(node.left, False, mem) + dfs(node.right, False, mem)
else:
mem[(node, taken)] = max(
dfs(node.left, True, mem) + dfs(node.right, True, mem) + node.val,
dfs(node.left, False, mem) + dfs(node.right, False, mem)
)
return mem[(node, taken)]
return dfs(root, False, dict())