Skip to content

Commit 58a5daf

Browse files
committed
feat: add solutions to lc problem: No.0337
No.0337.House Robber III
1 parent 9964e5d commit 58a5daf

File tree

6 files changed

+411
-15
lines changed

6 files changed

+411
-15
lines changed

solution/0300-0399/0337.House Robber III/README.md

Lines changed: 147 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,22 +41,168 @@
4141

4242
<!-- 这里可写通用的实现逻辑 -->
4343

44+
记忆化搜索。
45+
4446
<!-- tabs:start -->
4547

4648
### **Python3**
4749

4850
<!-- 这里可写当前语言的特殊实现逻辑 -->
4951

5052
```python
51-
53+
# Definition for a binary tree node.
54+
# class TreeNode:
55+
# def __init__(self, val=0, left=None, right=None):
56+
# self.val = val
57+
# self.left = left
58+
# self.right = right
59+
class Solution:
60+
def rob(self, root: TreeNode) -> int:
61+
@lru_cache(None)
62+
def dfs(root):
63+
if root is None:
64+
return 0
65+
if root.left is None and root.right is None:
66+
return root.val
67+
a = dfs(root.left) + dfs(root.right)
68+
b = root.val
69+
if root.left:
70+
b += dfs(root.left.left) + dfs(root.left.right)
71+
if root.right:
72+
b += dfs(root.right.left) + dfs(root.right.right)
73+
return max(a, b)
74+
75+
return dfs(root)
5276
```
5377

5478
### **Java**
5579

5680
<!-- 这里可写当前语言的特殊实现逻辑 -->
5781

5882
```java
83+
/**
84+
* Definition for a binary tree node.
85+
* public class TreeNode {
86+
* int val;
87+
* TreeNode left;
88+
* TreeNode right;
89+
* TreeNode() {}
90+
* TreeNode(int val) { this.val = val; }
91+
* TreeNode(int val, TreeNode left, TreeNode right) {
92+
* this.val = val;
93+
* this.left = left;
94+
* this.right = right;
95+
* }
96+
* }
97+
*/
98+
class Solution {
99+
private Map<TreeNode, Integer> memo;
100+
101+
public int rob(TreeNode root) {
102+
memo = new HashMap<>();
103+
return dfs(root);
104+
}
105+
106+
private int dfs(TreeNode root) {
107+
if (root == null) {
108+
return 0;
109+
}
110+
if (memo.containsKey(root)) {
111+
return memo.get(root);
112+
}
113+
int a = dfs(root.left) + dfs(root.right);
114+
int b = root.val;
115+
if (root.left != null) {
116+
b += dfs(root.left.left) + dfs(root.left.right);
117+
}
118+
if (root.right != null) {
119+
b += dfs(root.right.left) + dfs(root.right.right);
120+
}
121+
int res = Math.max(a, b);
122+
memo.put(root, res);
123+
return res;
124+
}
125+
}
126+
```
127+
128+
### **C++**
129+
130+
```cpp
131+
/**
132+
* Definition for a binary tree node.
133+
* struct TreeNode {
134+
* int val;
135+
* TreeNode *left;
136+
* TreeNode *right;
137+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
138+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
139+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
140+
* };
141+
*/
142+
class Solution {
143+
public:
144+
unordered_map<TreeNode*, int> memo;
145+
146+
int rob(TreeNode* root) {
147+
return dfs(root);
148+
}
149+
150+
int dfs(TreeNode* root) {
151+
if (!root) return 0;
152+
if (memo.count(root)) return memo[root];
153+
int a = dfs(root->left) + dfs(root->right);
154+
int b = root-> val;
155+
if (root->left) b += dfs(root->left->left) + dfs(root->left->right);
156+
if (root->right) b += dfs(root->right->left) + dfs(root->right->right);
157+
int res = max(a, b);
158+
memo[root] = res;
159+
return res;
160+
}
161+
};
162+
```
59163
164+
### **Go**
165+
166+
```go
167+
/**
168+
* Definition for a binary tree node.
169+
* type TreeNode struct {
170+
* Val int
171+
* Left *TreeNode
172+
* Right *TreeNode
173+
* }
174+
*/
175+
func rob(root *TreeNode) int {
176+
memo := make(map[*TreeNode]int)
177+
var dfs func(root *TreeNode) int
178+
dfs = func(root *TreeNode) int {
179+
if root == nil {
180+
return 0
181+
}
182+
if _, ok := memo[root]; ok {
183+
return memo[root]
184+
}
185+
a := dfs(root.Left) + dfs(root.Right)
186+
b := root.Val
187+
if root.Left != nil {
188+
b += dfs(root.Left.Left) + dfs(root.Left.Right)
189+
}
190+
if root.Right != nil {
191+
b += dfs(root.Right.Left) + dfs(root.Right.Right)
192+
}
193+
res := max(a, b)
194+
memo[root] = res
195+
return res
196+
}
197+
return dfs(root)
198+
}
199+
200+
func max(a, b int) int {
201+
if a > b {
202+
return a
203+
}
204+
return b
205+
}
60206
```
61207

62208
### **...**

solution/0300-0399/0337.House Robber III/README_EN.md

Lines changed: 145 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,157 @@
4242
### **Python3**
4343

4444
```python
45-
45+
# Definition for a binary tree node.
46+
# class TreeNode:
47+
# def __init__(self, val=0, left=None, right=None):
48+
# self.val = val
49+
# self.left = left
50+
# self.right = right
51+
class Solution:
52+
def rob(self, root: TreeNode) -> int:
53+
@lru_cache(None)
54+
def dfs(root):
55+
if root is None:
56+
return 0
57+
if root.left is None and root.right is None:
58+
return root.val
59+
a = dfs(root.left) + dfs(root.right)
60+
b = root.val
61+
if root.left:
62+
b += dfs(root.left.left) + dfs(root.left.right)
63+
if root.right:
64+
b += dfs(root.right.left) + dfs(root.right.right)
65+
return max(a, b)
66+
67+
return dfs(root)
4668
```
4769

4870
### **Java**
4971

5072
```java
73+
/**
74+
* Definition for a binary tree node.
75+
* public class TreeNode {
76+
* int val;
77+
* TreeNode left;
78+
* TreeNode right;
79+
* TreeNode() {}
80+
* TreeNode(int val) { this.val = val; }
81+
* TreeNode(int val, TreeNode left, TreeNode right) {
82+
* this.val = val;
83+
* this.left = left;
84+
* this.right = right;
85+
* }
86+
* }
87+
*/
88+
class Solution {
89+
private Map<TreeNode, Integer> memo;
90+
91+
public int rob(TreeNode root) {
92+
memo = new HashMap<>();
93+
return dfs(root);
94+
}
95+
96+
private int dfs(TreeNode root) {
97+
if (root == null) {
98+
return 0;
99+
}
100+
if (memo.containsKey(root)) {
101+
return memo.get(root);
102+
}
103+
int a = dfs(root.left) + dfs(root.right);
104+
int b = root.val;
105+
if (root.left != null) {
106+
b += dfs(root.left.left) + dfs(root.left.right);
107+
}
108+
if (root.right != null) {
109+
b += dfs(root.right.left) + dfs(root.right.right);
110+
}
111+
int res = Math.max(a, b);
112+
memo.put(root, res);
113+
return res;
114+
}
115+
}
116+
```
117+
118+
### **C++**
119+
120+
```cpp
121+
/**
122+
* Definition for a binary tree node.
123+
* struct TreeNode {
124+
* int val;
125+
* TreeNode *left;
126+
* TreeNode *right;
127+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
128+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
129+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
130+
* };
131+
*/
132+
class Solution {
133+
public:
134+
unordered_map<TreeNode*, int> memo;
135+
136+
int rob(TreeNode* root) {
137+
return dfs(root);
138+
}
139+
140+
int dfs(TreeNode* root) {
141+
if (!root) return 0;
142+
if (memo.count(root)) return memo[root];
143+
int a = dfs(root->left) + dfs(root->right);
144+
int b = root-> val;
145+
if (root->left) b += dfs(root->left->left) + dfs(root->left->right);
146+
if (root->right) b += dfs(root->right->left) + dfs(root->right->right);
147+
int res = max(a, b);
148+
memo[root] = res;
149+
return res;
150+
}
151+
};
152+
```
51153
154+
### **Go**
155+
156+
```go
157+
/**
158+
* Definition for a binary tree node.
159+
* type TreeNode struct {
160+
* Val int
161+
* Left *TreeNode
162+
* Right *TreeNode
163+
* }
164+
*/
165+
func rob(root *TreeNode) int {
166+
memo := make(map[*TreeNode]int)
167+
var dfs func(root *TreeNode) int
168+
dfs = func(root *TreeNode) int {
169+
if root == nil {
170+
return 0
171+
}
172+
if _, ok := memo[root]; ok {
173+
return memo[root]
174+
}
175+
a := dfs(root.Left) + dfs(root.Right)
176+
b := root.Val
177+
if root.Left != nil {
178+
b += dfs(root.Left.Left) + dfs(root.Left.Right)
179+
}
180+
if root.Right != nil {
181+
b += dfs(root.Right.Left) + dfs(root.Right.Right)
182+
}
183+
res := max(a, b)
184+
memo[root] = res
185+
return res
186+
}
187+
return dfs(root)
188+
}
189+
190+
func max(a, b int) int {
191+
if a > b {
192+
return a
193+
}
194+
return b
195+
}
52196
```
53197

54198
### **...**
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
8+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
9+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
10+
* };
11+
*/
12+
class Solution {
13+
public:
14+
unordered_map<TreeNode*, int> memo;
15+
16+
int rob(TreeNode* root) {
17+
return dfs(root);
18+
}
19+
20+
int dfs(TreeNode* root) {
21+
if (!root) return 0;
22+
if (memo.count(root)) return memo[root];
23+
int a = dfs(root->left) + dfs(root->right);
24+
int b = root-> val;
25+
if (root->left) b += dfs(root->left->left) + dfs(root->left->right);
26+
if (root->right) b += dfs(root->right->left) + dfs(root->right->right);
27+
int res = max(a, b);
28+
memo[root] = res;
29+
return res;
30+
}
31+
};

0 commit comments

Comments
 (0)