题目
我们想要使用一棵四叉树来储存一个 N x N 的布尔值网络。网络中每一格的值只会是真或假。树的根结点代表整个网络。对于每个结点, 它将被分等成四个孩子结点直到这个区域内的值都是相同的.
每个结点还有另外两个布尔变量: isLeaf 和 val。isLeaf 当这个节点是一个叶子结点时为真。val 变量储存叶子结点所代表的区域的值。
你的任务是使用一个四叉树表示给定的网络。下面的例子将有助于你理解这个问题:
给定下面这个8 x 8 网络,我们将这样建立一个对应的四叉树:
由上文的定义,它能被这样分割:
对应的四叉树应该像下面这样,每个结点由一对 (isLeaf, val) 所代表.
对于非叶子结点,val 可以是任意的,所以使用 * 代替。
提示:
- N 将小于 1000 且确保是 2 的整次幂。
- 如果你想了解更多关于四叉树的知识,你可以参考这个 wiki 页面。
代码模板:
/*
// Definition for a QuadTree node.
class Node {
public boolean val;
public boolean isLeaf;
public Node topLeft;
public Node topRight;
public Node bottomLeft;
public Node bottomRight;
public Node() {}
public Node(boolean _val,boolean _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(int[][] grid) {
}
}
分析
这道题目很懵。引用一下别人的总结。
这道题将一个大区域划分为若干个小区域(要求每个小区域中的val全部一致),每次都是将一个大区域划分为4份(直到小区域val全部一致,终止)。
- 按照声明中的topLeft、topRight、bottomLeft以及bottomRight将一个大区域划分为4各小区域。
- 使用for循环校验每个小区域中val是否全部一致。
- 如果小区域不一致,则为非叶子结点,还可以继续划分(此时val为任意bool值)。
- 如果小区域全部一致,则isLeaf此时设置val、isLeaf=true同时四个指针全部置空
解答
/*
// Definition for a QuadTree node.
class Node {
public boolean val;
public boolean isLeaf;
public Node topLeft;
public Node topRight;
public Node bottomLeft;
public Node bottomRight;
public Node() {}
public Node(boolean _val,boolean _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(int[][] grid) {
return fun(grid,0,grid[0].length,0,grid.length);
}
public Node fun(int[][] grid,int left,int right,int top,int bottom){
Node root = null;
int key = grid[top][left];
for(int i=top;i<bottom;i++){
for(int j=left;j<right;j++){
if(grid[i][j]!=key){
Node topLeft = fun(grid,left,(left+right)/2,top,(top+bottom)/2);
Node topRight = fun(grid,(left+right)/2,right,top,(top+bottom)/2);
Node bottomLeft = fun(grid,left,(left+right)/2,(top+bottom)/2,bottom);
Node bottomRight = fun(grid,(left+right)/2,right,(top+bottom)/2,bottom);
root = new Node(false,false,topLeft,topRight,bottomLeft,bottomRight);
return root;
}
}
}
root = new Node();
root.val = key==1?true:false;
root.isLeaf = true;
return root;
}
}