-
Notifications
You must be signed in to change notification settings - Fork 0
/
337.py
50 lines (38 loc) · 1.48 KB
/
337.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = ['"wuyadong" <[email protected]>']
# Definition for a binary tree node.
class TreeNode(object):
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Solution(object):
def __init__(self):
self.cache = {}
def rob(self, root):
"""
:type root: TreeNode
:rtype: int
"""
if root is None:
return 0
if root.left not in self.cache:
self.cache[root.left] = self.rob(root.left)
if root.right not in self.cache:
self.cache[root.right] = self.rob(root.right)
r1 = self.cache[root.left] + self.cache[root.right]
r2 = root.val
if root.left is not None:
if root.left.left not in self.cache:
self.cache[root.left.left] = self.rob(root.left.left)
if root.left.right not in self.cache:
self.cache[root.left.right] = self.rob(root.left.right)
r2 += self.cache[root.left.left] + self.cache[root.left.right]
if root.right is not None:
if root.right.left not in self.cache:
self.cache[root.right.left] = self.rob(root.right.left)
if root.right.right not in self.cache:
self.cache[root.right.right] = self.rob(root.right.right)
r2 += self.cache[root.right.left] + self.cache[root.right.right]
return r1 if r1 > r2 else r2