Construct Quad Tree - Complete Solution Guide
Construct Quad Tree is LeetCode problem 427, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
Given a n * n matrix grid of 0's and 1's 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 1's or False if the node represents a grid of 0's. 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 th
Detailed Explanation
The problem requires constructing a Quad-Tree from a given n x n grid of 0s and 1s. A Quad-Tree is a tree where each internal node has exactly four children, representing four quadrants of the grid. A node is a leaf node if the corresponding grid section contains only 0s or only 1s. In that case, the 'val' attribute of the leaf node is set to the value of the grid (0 or 1), and 'isLeaf' is set to true. If the grid section contains both 0s and 1s, the node is an internal node; 'isLeaf' is set to false, the 'val' attribute can be either 0 or 1 (it doesn't matter for internal nodes), and the grid is divided into four equal sub-grids, for which four child Quad-Trees are recursively constructed (topLeft, topRight, bottomLeft, bottomRight). The input is a 2D array (matrix) of integers representing the grid. The output is the root of the constructed Quad-Tree.
Solution Approach
The solution uses a recursive Depth-First Search (DFS) approach to construct the Quad-Tree. The core idea is to repeatedly divide the grid into four quadrants until a quadrant contains only a single value (all 0s or all 1s). The `dfs` function takes the starting row, starting column, and size of the current sub-grid as input. It first checks if all the elements in the current sub-grid are the same. If they are, it creates a leaf node with the corresponding value and sets `isLeaf` to true. Otherwise, it divides the sub-grid into four equal quadrants, recursively calls itself to build the Quad-Trees for each quadrant, and then creates an internal node with these four subtrees as its children.
Step-by-Step Algorithm
- Step 1: Define a recursive function `dfs(r, c, size)` that takes the starting row `r`, starting column `c`, and the size of the sub-grid `size` as input.
- Step 2: Check if all elements in the current sub-grid (from `grid[r][c]` to `grid[r+size-1][c+size-1]`) are the same. This involves iterating through all elements in the sub-grid.
- Step 3: If all elements are the same, create a new `Node` with `isLeaf = true` and `val` equal to the value of the elements in the sub-grid (0 or 1). Return the created node.
- Step 4: If the elements are not all the same, divide the sub-grid into four equal quadrants, each with size `size/2`.
- Step 5: Recursively call `dfs` for each of the four quadrants: top-left (r, c, size/2), top-right (r, c + size/2, size/2), bottom-left (r + size/2, c, size/2), and bottom-right (r + size/2, c + size/2, size/2).
- Step 6: Create a new `Node` with `isLeaf = false`, and set its `topLeft`, `topRight`, `bottomLeft`, and `bottomRight` children to the nodes returned by the recursive calls. The `val` field of the internal node can be any value (typically true or false). Return this internal node.
- Step 7: Call the `dfs` function initially with `r = 0`, `c = 0`, and `size = len(grid)` to start the Quad-Tree construction from the entire grid.
Key Insights
- Insight 1: The problem lends itself perfectly to a divide-and-conquer (recursion) approach because the Quad-Tree structure naturally mirrors the division of the grid into quadrants.
- Insight 2: Determining whether a sub-grid contains all the same values is crucial for identifying leaf nodes, which halts the recursion for that branch.
- Insight 3: The choice of 'val' for internal nodes is arbitrary as long as 'isLeaf' is false. It's more important to correctly build the subtrees.
- Insight 4: Understanding the base case for recursion is key - when a subgrid contains only one value.
Complexity Analysis
Time Complexity: O(n^2)
Space Complexity: O(n^2)
Topics
This problem involves: Array, Divide and Conquer, Tree, Matrix.
Companies
Asked at: Palantir Technologies.