You are given a square matrix ‘matrix of size ‘N’ containing 0’s and 1’s only. You have to represent the matrix as a Quad-Tree.
A Quad-Tree is a tree data structure where each node has exactly four children 'topLeft', 'topRight', 'bottomLeft', 'bottomRight', 'bottomLeft'. All nodes have two attributes, 'value': 1 if the node represents a grid of 1's or 0 if the node represents a grid of 0's and 'isLeaf': true if the node is leaf node on the tree or false if the node has the four children.
We can construct a Quad-Tree from a two-dimensional area using the following steps:
If the current grid has the same value (i.e., all 1's or all 0's) set isLeaf to true and set 'value' to the value of the grid and set the four children to null and stop.
If the current grid has different values, set “isLeaf” to false and set value to -1, and divide the current grid into four sub-grids as shown in the image.
Repeat the above steps for each of the children with the proper sub-grid.

The first line contains an integer 'T' which denotes the number of test cases or queries to be run. Then the test cases follow.
The 1st line of each test case contains one integer 'N', representing the size of the square matrix.
The next 'N' lines contain 'N' integers separated by spaces describing rows of matrix 'matrix' (each element of ‘matrix’ is either 0 or 1).
Output Format
The output represents the Quad-Tree in a level order manner, with each node represented as a comma-separated value isLeaf, value.
If the value of “isLeaf” or “value” is 1 we represent it as 1, 1, and if the value of “isLeaf” or “value” is 0 we represent it as 0, 0, and if the “value” is -1 we represent it as -1.
Note :
You do not need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 10
N = 2^y where 0 <= y <= 10
matrix[i][j] = 0 or 1
Time Limit: 1 second
2
2
0 1
1 0
8
1 1 1 1 0 0 1 1
1 1 1 1 0 0 1 1
1 1 1 1 1 1 0 0
1 1 1 1 1 1 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
1 1 1 1 0 0 0 0
0, -1 1, 0 1, 1 1, 1 1, 0
0, -1 1, 1 0, -1 1, 1 1, 0 1, 0 1, 1 1, 1 1, 0
For the first test case:
At level 0: [0, -1].
At level 1: [1, 0], [1, 1], [1,1], [1, 0].
For the second test case:
All values in the grid are not the same. We divide the grid into four sub-grids. The topLeft, bottomLeft, and bottomRight each have the same value.
The topRight has different values so we divide it into 4 sub-grids where each has the same value.
At level 0 : [0, -1].
At level 1 : [1, 1], [0, -1], [0, 1], [1, 1], [1, 0].
At level 2 : [1, 0], [1, 1], [1, 1], [1, 0].
2
2
1 1
1 1
1
0
1, 1
1, 0
Can you think of some recursive technique to divide the grid into equal parts?
The idea here is to divide the grid into four equal parts the topLeft, topRight, bottomLeft, bottomRight. And then construct the Quad-Tree in a recursive manner. All four children of a node determine whether it should be a leaf node or not.
A currentNode is a leaf node only if all of its four children’s topLeft.value, topRight.value, bottomLeft.value, bottomRight.value are equal and topLeft.isLeaf, topRight.leaf, bottomLeft.leaf, bottomRight.leaf are true. Then we set current.value to any of its children’s value and set currentNode.isLeaf to True.
Otherwise, set currentNode.value to either 0 or 1 and set currentNode.isLeaf to False, and connect the four children links topLeft, topRight, bottomLeft, bottomRight to currentNode.
The algorithm is as follows:
O(N ^ 2), Where ‘N’ is the size in the given grid.
Since we are visiting each cell twice which is of order 2 * N * N. So, the overall time complexity is O(N ^ N).
O(log(N)), Where ‘N’ is the size in the given grid.
Since the maximum recursive call stack is of order log4N. So, the overall space complexity is O(logN).