-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconstructQuadTree.cpp
70 lines (63 loc) · 1.99 KB
/
constructQuadTree.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Source: https://leetcode.com/problems/construct-quad-tree/
// Author: Miao Zhang
// Date: 2021-02-10
/*
// Definition for a QuadTree node.
class Node {
public:
bool val;
bool isLeaf;
Node* topLeft;
Node* topRight;
Node* bottomLeft;
Node* bottomRight;
Node() {
val = false;
isLeaf = false;
topLeft = NULL;
topRight = NULL;
bottomLeft = NULL;
bottomRight = NULL;
}
Node(bool _val, bool _isLeaf) {
val = _val;
isLeaf = _isLeaf;
topLeft = NULL;
topRight = NULL;
bottomLeft = NULL;
bottomRight = NULL;
}
Node(bool _val, bool _isLeaf, Node* _topLeft, Node* _topRight, Node* _bottomLeft, Node* _bottomRight) {
val = _val;
isLeaf = _isLeaf;
topLeft = _topLeft;
topRight = _topRight;
bottomLeft = _bottomLeft;
bottomRight = _bottomRight;
}
};
*/
class Solution {
public:
Node* construct(vector<vector<int>>& grid) {
return cons(grid, 0, 0, grid.size());
}
private:
Node* cons(vector<vector<int>>& grid, int i, int j, int n) {
if (n == 0) return nullptr;
if (n == 1) return new Node(grid[i][j], true, nullptr, nullptr, nullptr, nullptr);
auto topleft = cons(grid, i, j, n / 2);
auto topright = cons(grid, i, j + n / 2, n / 2);
auto bottomleft = cons(grid, i + n / 2, j, n / 2);
auto bottomright = cons(grid, i + n / 2, j + n / 2, n / 2);
if (topleft->isLeaf && topright->isLeaf && bottomleft->isLeaf && bottomright->isLeaf && topleft->val == topright->val && topleft->val == bottomleft->val && topleft->val == bottomright->val) {
auto root = new Node(topleft->val, true, nullptr, nullptr, nullptr, nullptr);
delete topleft;
delete topright;
delete bottomleft;
delete bottomright;
return root;
}
return new Node(true, false, topleft, topright, bottomleft, bottomright);
}
};