@@ -105,22 +105,243 @@ topRight 具有不同的值,因此我们将其再分为 4 个子网格,这
105
105
106
106
<!-- 这里可写通用的实现逻辑 -->
107
107
108
+ ** 方法一:DFS**
109
+
110
+ DFS 递归遍历 grid,先判断 grid 是否为叶子节点,是则返回叶子节点相关信息;否则递归 grid 4 个子节点。
111
+
108
112
<!-- tabs:start -->
109
113
110
114
### ** Python3**
111
115
112
116
<!-- 这里可写当前语言的特殊实现逻辑 -->
113
117
114
118
``` python
115
-
119
+ """
120
+ # Definition for a QuadTree node.
121
+ class Node:
122
+ def __init__(self, val, isLeaf, topLeft, topRight, bottomLeft, bottomRight):
123
+ self.val = val
124
+ self.isLeaf = isLeaf
125
+ self.topLeft = topLeft
126
+ self.topRight = topRight
127
+ self.bottomLeft = bottomLeft
128
+ self.bottomRight = bottomRight
129
+ """
130
+
131
+
132
+ class Solution :
133
+ def construct (self , grid : List[List[int ]]) -> ' Node' :
134
+ def dfs (a , b , c , d ):
135
+ zero = one = 0
136
+ for i in range (a, c + 1 ):
137
+ for j in range (b, d + 1 ):
138
+ if grid[i][j] == 0 :
139
+ zero = 1
140
+ else :
141
+ one = 1
142
+ isLeaf = zero + one == 1
143
+ val = isLeaf and one
144
+ if isLeaf:
145
+ return Node(grid[a][b], True )
146
+ topLeft = dfs(a, b, (a + c) // 2 , (b + d) // 2 )
147
+ topRight = dfs(a, (b + d) // 2 + 1 , (a + c) // 2 , d)
148
+ bottomLeft = dfs((a + c) // 2 + 1 , b, c, (b + d) // 2 )
149
+ bottomRight = dfs((a + c) // 2 + 1 , (b + d) // 2 + 1 , c, d)
150
+ return Node(val, isLeaf, topLeft, topRight, bottomLeft, bottomRight)
151
+
152
+ return dfs(0 , 0 , len (grid) - 1 , len (grid[0 ]) - 1 )
116
153
```
117
154
118
155
### ** Java**
119
156
120
157
<!-- 这里可写当前语言的特殊实现逻辑 -->
121
158
122
159
``` java
160
+ /*
161
+ // Definition for a QuadTree node.
162
+ class Node {
163
+ public boolean val;
164
+ public boolean isLeaf;
165
+ public Node topLeft;
166
+ public Node topRight;
167
+ public Node bottomLeft;
168
+ public Node bottomRight;
169
+
170
+
171
+ public Node() {
172
+ this.val = false;
173
+ this.isLeaf = false;
174
+ this.topLeft = null;
175
+ this.topRight = null;
176
+ this.bottomLeft = null;
177
+ this.bottomRight = null;
178
+ }
179
+
180
+ public Node(boolean val, boolean isLeaf) {
181
+ this.val = val;
182
+ this.isLeaf = isLeaf;
183
+ this.topLeft = null;
184
+ this.topRight = null;
185
+ this.bottomLeft = null;
186
+ this.bottomRight = null;
187
+ }
188
+
189
+ public Node(boolean val, boolean isLeaf, Node topLeft, Node topRight, Node bottomLeft, Node bottomRight) {
190
+ this.val = val;
191
+ this.isLeaf = isLeaf;
192
+ this.topLeft = topLeft;
193
+ this.topRight = topRight;
194
+ this.bottomLeft = bottomLeft;
195
+ this.bottomRight = bottomRight;
196
+ }
197
+ };
198
+ */
199
+
200
+ class Solution {
201
+ public Node construct (int [][] grid ) {
202
+ return dfs(0 , 0 , grid. length - 1 , grid[0 ]. length - 1 , grid);
203
+ }
204
+
205
+ private Node dfs (int a , int b , int c , int d , int [][] grid ) {
206
+ int zero = 0 , one = 0 ;
207
+ for (int i = a; i <= c; ++ i) {
208
+ for (int j = b; j <= d; ++ j) {
209
+ if (grid[i][j] == 0 ) {
210
+ zero = 1 ;
211
+ } else {
212
+ one = 1 ;
213
+ }
214
+ }
215
+ }
216
+ boolean isLeaf = zero + one == 1 ;
217
+ boolean val = isLeaf && one == 1 ;
218
+ Node node = new Node (val, isLeaf);
219
+ if (isLeaf) {
220
+ return node;
221
+ }
222
+ node. topLeft = dfs(a, b, (a + c) / 2 , (b + d) / 2 , grid);
223
+ node. topRight = dfs(a, (b + d) / 2 + 1 , (a + c) / 2 , d, grid);
224
+ node. bottomLeft = dfs((a + c) / 2 + 1 , b, c, (b + d) / 2 , grid);
225
+ node. bottomRight = dfs((a + c) / 2 + 1 , (b + d) / 2 + 1 , c, d, grid);
226
+ return node;
227
+ }
228
+ }
229
+ ```
230
+
231
+ ### ** C++**
232
+
233
+ ``` cpp
234
+ /*
235
+ // Definition for a QuadTree node.
236
+ class Node {
237
+ public:
238
+ bool val;
239
+ bool isLeaf;
240
+ Node* topLeft;
241
+ Node* topRight;
242
+ Node* bottomLeft;
243
+ Node* bottomRight;
244
+
245
+ Node() {
246
+ val = false;
247
+ isLeaf = false;
248
+ topLeft = NULL;
249
+ topRight = NULL;
250
+ bottomLeft = NULL;
251
+ bottomRight = NULL;
252
+ }
253
+
254
+ Node(bool _val, bool _isLeaf) {
255
+ val = _val;
256
+ isLeaf = _isLeaf;
257
+ topLeft = NULL;
258
+ topRight = NULL;
259
+ bottomLeft = NULL;
260
+ bottomRight = NULL;
261
+ }
262
+
263
+ Node(bool _val, bool _isLeaf, Node* _topLeft, Node* _topRight, Node* _bottomLeft, Node* _bottomRight) {
264
+ val = _val;
265
+ isLeaf = _isLeaf;
266
+ topLeft = _topLeft;
267
+ topRight = _topRight;
268
+ bottomLeft = _bottomLeft;
269
+ bottomRight = _bottomRight;
270
+ }
271
+ };
272
+ */
273
+
274
+ class Solution {
275
+ public:
276
+ Node* construct(vector<vector<int >>& grid) {
277
+ return dfs(0, 0, grid.size() - 1, grid[ 0] .size() - 1, grid);
278
+ }
279
+
280
+ Node* dfs(int a, int b, int c, int d, vector<vector<int>>& grid) {
281
+ int zero = 0, one = 0;
282
+ for (int i = a; i <= c; ++i)
283
+ {
284
+ for (int j = b; j <= d; ++j)
285
+ {
286
+ if (grid[i][j]) one = 1;
287
+ else zero = 1;
288
+ }
289
+ }
290
+ bool isLeaf = zero + one == 1 ;
291
+ bool val = isLeaf && one;
292
+ Node* node = new Node(val, isLeaf);
293
+ if (isLeaf) return node;
294
+ node->topLeft = dfs(a, b, (a + c) / 2 , (b + d) / 2 , grid);
295
+ node->topRight = dfs(a, (b + d) / 2 + 1 , (a + c) / 2 , d, grid);
296
+ node->bottomLeft = dfs((a + c) / 2 + 1 , b, c, (b + d) / 2 , grid);
297
+ node->bottomRight = dfs((a + c) / 2 + 1 , (b + d) / 2 + 1 , c, d, grid);
298
+ return node;
299
+ }
300
+ };
301
+ ```
123
302
303
+ ### ** Go**
304
+
305
+ ``` go
306
+ /* *
307
+ * Definition for a QuadTree node.
308
+ * type Node struct {
309
+ * Val bool
310
+ * IsLeaf bool
311
+ * TopLeft *Node
312
+ * TopRight *Node
313
+ * BottomLeft *Node
314
+ * BottomRight *Node
315
+ * }
316
+ */
317
+
318
+ func construct (grid [][]int ) *Node {
319
+ var dfs func (a, b, c, d int ) *Node
320
+ dfs = func (a, b, c, d int ) *Node {
321
+ zero , one := 0 , 0
322
+ for i := a; i <= c; i++ {
323
+ for j := b; j <= d; j++ {
324
+ if grid[i][j] == 0 {
325
+ zero = 1
326
+ } else {
327
+ one = 1
328
+ }
329
+ }
330
+ }
331
+ isLeaf := zero+one == 1
332
+ val := isLeaf && one == 1
333
+ node := &Node{Val: val, IsLeaf: isLeaf}
334
+ if isLeaf {
335
+ return node
336
+ }
337
+ node.TopLeft = dfs (a, b, (a+c)/2 , (b+d)/2 )
338
+ node.TopRight = dfs (a, (b+d)/2 +1 , (a+c)/2 , d)
339
+ node.BottomLeft = dfs ((a+c)/2 +1 , b, c, (b+d)/2 )
340
+ node.BottomRight = dfs ((a+c)/2 +1 , (b+d)/2 +1 , c, d)
341
+ return node
342
+ }
343
+ return dfs (0 , 0 , len (grid)-1 , len (grid[0 ])-1 )
344
+ }
124
345
```
125
346
126
347
### ** ...**
0 commit comments