题目:
We want to use quad trees to store an
N x N
boolean grid. Each cell in the grid can only be true or false. The root node represents the whole grid. For each node, it will be subdivided into four children nodes until the values in the region it represents are all the same.
Each node has another two boolean attributes :isLeaf
andval
.isLeaf
is true if and only if the node is a leaf node. Theval
attribute for a leaf node contains the value of the region it represents.
Your task is to use a quad tree to represent a given grid. The following example may help you understand the problem better:
Given the8 x 8
grid below, we want to construct the corresponding quad tree:
It can be divided according to the definition above:
The corresponding quad tree should be as following, where each node is
represented as a(isLeaf, val)
pair.
For the non-leaf nodes,val
can be arbitrary, so it is represented as*
.
Note:
N
is less than1000
and guaranteened to be a power of 2. If you
want to know more about the quad tree, you can refer to its wiki.
解释:
四分树,一个node有4个子node,直到这个node的所有子node所代表的值完全一样才停止
dfs 递归
x向下 y向右
非叶子节点的val是任意的,而不是*
"""
# Definition for a QuadTree node.
class Node(object):
def __init__(self, val, isLeaf, topLeft, topRight, bottomLeft, bottomRight):
self.val = val
self.isLeaf = isLeaf
self.topLeft = topLeft
self.topRight = topRight
self.bottomLeft = bottomLeft
self.bottomRight = bottomRight
"""
class Solution(object):
def construct(self, grid):
"""
:type grid: List[List[int]]
:rtype: Node
"""
def dfs(x,y,l):
if l==1:
node=Node(grid[x][y]==1,True,None,None,None,None)
else:
topLeft=dfs(x,y,l//2)
topRight=dfs(x,y+l//2,l//2)
bottomLeft=dfs(x+l//2,y,l//2)
bottomRight=dfs(x+l//2,y+l//2,l//2)
value=topLeft.val or topRight.val or bottomLeft.val or bottomRight.val
if topLeft.isLeaf and topRight.isLeaf and bottomLeft.isLeaf and bottomRight.isLeaf and topLeft.val==topRight.val==bottomLeft.val==bottomRight.val:
node=Node(value,True,None,None,None,None)
else:
node=Node(value,False,topLeft,topRight,bottomLeft,bottomRight)
return node
return grid and dfs(0,0,len(grid)) or None
c++代码:
/*
// Definition for a QuadTree node.
class Node {
public:
bool val;
bool isLeaf;
Node* topLeft;
Node* topRight;
Node* bottomLeft;
Node* bottomRight;
Node() {}
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 dfs(0,0,grid.size(),grid);
}
Node* dfs(int x,int y,int l,vector<vector<int>>&grid)
{ Node* node;
if(l==1)
{
node=new Node(grid[x][y]==1,true,NULL,NULL,NULL,NULL);
}
else
{
Node* topLeft=dfs(x,y,l/2,grid);
Node* topRight=dfs(x,y+l/2,l/2,grid);
Node* bottomLeft=dfs(x+l/2,y,l/2,grid);
Node* bottomRight=dfs(x+l/2,y+l/2,l/2,grid);
int value=(topLeft->val ||topRight->val||bottomLeft->val||bottomRight->val);
if ((topLeft->isLeaf) &&(topRight->isLeaf) &&(bottomLeft->isLeaf) && (bottomRight->isLeaf)&&((topLeft->val==topRight->val)&&(topRight->val==bottomLeft->val)&&(bottomLeft->val==bottomRight->val)))
{
node=new Node(value,true,NULL,NULL,NULL,NULL);
}
else
{
node=new Node(value,false,topLeft,topRight,bottomLeft,bottomRight);
}
}
return node;
}
};
c++代码不知道为啥过不了…Disscuss里面过的也少,是不是测试用例有问题?
结果发现是逻辑判断错了,注意c++逻辑运算的时候不像python那么方便,尤其不能连着使用==
,在不清楚的情况下一定不要简写而且要多用()
。
总结: