Skip to content

Commit d303aa9

Browse files
committed
feat(leetcode): add No.337
1 parent dd43984 commit d303aa9

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

337.House-Robber-III.py

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# https://leetcode.com/problems/house-robber-iii/
2+
#
3+
# algorithms
4+
# Medium (47.38%)
5+
# Total Accepted: 93,716
6+
# Total Submissions: 197,795
7+
8+
# Definition for a binary tree node.
9+
# class TreeNode(object):
10+
# def __init__(self, x):
11+
# self.val = x
12+
# self.left = None
13+
# self.right = None
14+
15+
class Solution(object):
16+
def rob(self, root):
17+
"""
18+
:type root: TreeNode
19+
:rtype: int
20+
"""
21+
if not root:
22+
return 0
23+
24+
return max(self.recursive(root))
25+
26+
def recursive(self, node):
27+
if not node:
28+
return 0, 0
29+
30+
l_pre, l_cur = self.recursive(node.left)
31+
r_pre, r_cur = self.recursive(node.right)
32+
33+
pre = l_cur + r_cur
34+
cur = max(node.val + l_pre + r_pre, pre)
35+
36+
return pre, cur
37+

0 commit comments

Comments
 (0)