-
Notifications
You must be signed in to change notification settings - Fork 1
/
construct-quad-tree.cpp
32 lines (30 loc) · 1.28 KB
/
construct-quad-tree.cpp
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
// https://leetcode.com/problems/construct-quad-tree/
class Solution {
public:
Node* quadTree(vector<vector<int>>& grid, int row_start, int row_end, int col_start, int col_end){
if(row_start > row_end || col_start > col_end) return NULL;
bool flag = true;
int check = grid[row_start][col_start];
for(int i = row_start; i <= row_end; i++){
for(int j = col_start; j <= col_end; j++){
if(check != grid[i][j]){
flag = false;
break;
}
}
if(!flag) break;
}
if(flag) return new Node(check, true);
int row_mid = (row_start + row_end) / 2;
int col_mid = (col_start + col_end) / 2;
Node* topLeft = quadTree(grid, row_start, row_mid, col_start, col_mid);
Node* topRight = quadTree(grid, row_start, row_mid, col_mid + 1, col_end);
Node* bottomLeft = quadTree(grid, row_mid + 1, row_end, col_start, col_mid);
Node* bottomRight = quadTree(grid, row_mid + 1, row_end, col_mid + 1, col_end);
return new Node(check, false, topLeft, topRight, bottomLeft, bottomRight);
}
Node* construct(vector<vector<int>>& grid) {
int n = grid.size();
return quadTree(grid, 0, n - 1, 0, n - 1);
}
};