← 返回 uber 的题目列表Construct a Quad Tree
类型:online_judge
Problem
Given an n x n binary matrix grid (where n is a power of 2), construct a Quad Tree and return its root.
Each node has:
val (boolean, meaningful if leaf)
isLeaf
topLeft, topRight, bottomLeft, bottomRight
Rules:
If all values in the current subgrid are the same, create a leaf node.
Otherwise create an internal node and recursively build 4 children from the 4 quadrants.
Input
First line: integer n
Next n lines: n integers (0/1) separated by spaces
Constraints
1 <= n <= 64
n is a power of 2
Example
Input
2
1 1
1 1
Output
leaf(val=1)