244. Construct Quad Tree
Given an n x n matrix grid of 0's and 1's (n is a power of two), build its Quad-Tree representation. A Quad-Tree node has a boolean val (the region's value) and a boolean isLeaf (true if the node represents a region of all the same value). A leaf's val is the common cell value; an internal node has exactly four children — topLeft, topRight, bottomLeft, bottomRight — each covering one quadrant. Build the tree recursively: if a region is uniform it becomes a leaf, otherwise split it into four equal quadrants. This catalog task returns the tree serialized in level order as a list of [isLeaf, val] pairs (1/0), where every internal node is followed by its four children.
Examples
Input: [[0,1],[1,0]]
Output: [[0,1],[1,0],[1,1],[1,1],[1,0]]
Explanation: The 2x2 grid isn't uniform, so the root is internal followed by its four single-cell leaves.
Constraints
- n == grid.length == grid[i].length; n == 2^x where 0 <= x <= 6; grid[i][j] is 0 or 1
Run checks all cases above. Submit evaluates all test cases.