

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).
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.
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
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: