力扣427 建立四叉树
给你一个 n * n 矩阵 grid ,矩阵由若干 0 和 1 组成。请你用四叉树表示该矩阵 grid 。
你需要返回能表示矩阵的 四叉树 的根结点。
注意,当 isLeaf 为 False 时,你可以把 True 或者 False 赋值给节点,两种值都会被判题机制 接受 。
四叉树数据结构中,每个内部节点只有四个子节点。此外,每个节点都有两个属性:
val:储存叶子结点所代表的区域的值。1 对应 True,0 对应 False;
isLeaf: 当这个节点是一个叶子结点时为 True,如果它有 4 个子节点则为 False 。
class Node {
public boolean val;
public boolean isLeaf;
public Node topLeft;
public Node topRight;
public Node bottomLeft;
public Node bottomRight;
}
我们可以按以下步骤为二维区域构建四叉树:
如果当前网格的值相同(即,全为 0 或者全为 1),将 isLeaf 设为 True ,将 val 设为网格相应的值,并将四个子节点都设为 Null 然后停止。
如果当前网格的值不同,将 isLeaf 设为 False, 将 val 设为任意值,然后如下图所示,将当前网格划分为四个子网格。
使用适当的子网格递归每个子节点。
如果你想了解更多关于四叉树的内容,可以参考 wiki 。
四叉树格式:
输出为使用层序遍历后四叉树的序列化形式,其中 null 表示路径终止符,其下面不存在节点。
它与二叉树的序列化非常相似。唯一的区别是节点以列表形式表示 [isLeaf, val] 。
如果 isLeaf 或者 val 的值为 True ,则表示它在列表 [isLeaf, val] 中的值为 1 ;如果 isLeaf 或者 val 的值为 False ,则表示值为 0 。
// 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;
}
};
DFS
class Solution {
public:
Node* construct(vector<vector<int>>& grid) {
int len=grid.size();
Node* ans=dfs(grid,0,len,0,len);
return ans;
}
Node* dfs(vector<vector<int>>& grid,int l1,int l2,int w1,int w2) {
Node* n=new Node;
if(l2-l1==1){
n->isLeaf=true;
n->val=grid[l1][w1];
return n;
}
int ml=(l1+l2)/2,mw=(w1+w2)/2;
n->topLeft=dfs(grid,l1,ml,w1,mw);
n->topRight=dfs(grid,l1,ml,mw,w2);
n->bottomLeft=dfs(grid,ml,l2,w1,mw);
n->bottomRight=dfs(grid,ml,l2,mw,w2);
vector<Node*> kids={n->topLeft,n->topRight,n->bottomLeft,n->bottomRight};
// judge every node
n->val=n->topLeft->val;
n->isLeaf=true;
for(auto i : kids){
if(i->isLeaf==false || i->val!=n->val){
n->isLeaf=false;
break;
}
}
// whether delete nodes?
if(n->isLeaf){
n->topLeft=nullptr;
n->topRight=nullptr;
n->bottomLeft=nullptr;
n->bottomRight=nullptr;
}
return n;
}
};
标答
class Solution {
public:
Node *construct(vector<vector<int>> &grid) {
int n = grid.size();
// 预处理
vector<vector<int>> pre(n + 1, vector<int>(n + 1));
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
pre[i][j] = pre[i - 1][j] + pre[i][j - 1] - pre[i - 1][j - 1] + grid[i - 1][j - 1];
}
}
auto getSum = [&](int r0, int c0, int r1, int c1) {
return pre[r1][c1] - pre[r1][c0] - pre[r0][c1] + pre[r0][c0];
};
function<Node *(int, int, int, int)> dfs = [&](int r0, int c0, int r1, int c1) {
int total = getSum(r0, c0, r1, c1);
if (total == 0) {
return new Node(false, true);
}
if (total == (r1 - r0) * (c1 - c0)) {
return new Node(true, true);
}
return new Node(
true,
false,
dfs(r0, c0, (r0 + r1) / 2, (c0 + c1) / 2),
dfs(r0, (c0 + c1) / 2, (r0 + r1) / 2, c1),
dfs((r0 + r1) / 2, c0, r1, (c0 + c1) / 2),
dfs((r0 + r1) / 2, (c0 + c1) / 2, r1, c1)
);
};
return dfs(0, 0, n, n);
}
};