We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent ee4b65f commit 3d077ceCopy full SHA for 3d077ce
house_robber_iii.py
@@ -0,0 +1,18 @@
1
+# -*- coding: utf-8 -*-
2
+
3
+class Solution:
4
+ # @param {TreeNode} root, the root of binary tree.
5
+ # @return {int} The maximum amount of money you can rob tonight
6
+ def houseRobber3(self, root):
7
+ # write your code here
8
+ # 递归!比较该节点抢和不抢的最大值。
9
+ return max(self._houseRobber3(root))
10
11
+ def _houseRobber3(self, root):
12
+ if not root:
13
+ return (0, 0)
14
+ rob_left = self._houseRobber3(root.left)
15
+ rob_right = self._houseRobber3(root.right)
16
+ not_rob = rob_left[1] + rob_right[1] + root.val
17
+ rob = max(rob_left) + max(rob_right)
18
+ return (not_rob, rob)
0 commit comments