Given a n * n matrix grid of 0s and 1s only. We want to represent grid with a Quad-Tree.
Return the root of the Quad-Tree representing grid.
A Quad-Tree is a tree data structure in which each internal node has exactly four children. Besides, each node has two attributes:
val: True if the node represents a grid of 1s or False if the node represents a grid of 0s. Notice that you can assign the val to True or False when isLeaf is False, and both are accepted in the answer.isLeaf: True if the node is a leaf node on the tree or False if the node has four children.We can construct a Quad-Tree from a two-dimensional area using the following steps:
1s or all 0s), set isLeaf to True and set val to the value of the grid and set the four children to None and stop.isLeaf to False and set val to any value and divide the current grid into four sub-grids.n == grid.length == grid[i].lengthn == 2^x where 0 <= x <= 6grid = [[0,1],[1,0]][[0,1],[1,0],[1,1],[1,1],[1,0]]grid = [[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0]][[0,1],[1,1],[0,1],[1,1],[1,0],null,null,null,null,[1,0],[1,0],[1,1],[1,1]]