LeetCode 427. Construct Quad Tree

原题链接在这里:https://leetcode.com/problems/construct-quad-tree/

题目:

Given a n * n matrix grid of 0's and 1's only. We want to represent the grid with a Quad-Tree.

Return the root of the Quad-Tree representing the grid.

Notice that you can assign the value of a node to True or False when isLeaf is False, and both are accepted in the answer.

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.
  • isLeaf: True if the node is leaf node on the tree or False if the node has the four children.
class Node {
    public boolean val;
    public boolean isLeaf;
    public Node topLeft;
    public Node topRight;
    public Node bottomLeft;
    public Node bottomRight;
}

We can construct a Quad-Tree from a two-dimensional area using the following steps:

  1. If the current grid has the same value (i.e all 1's or all 0's) set isLeaf True and set val to the value of the grid and set the four children to Null and stop.
  2. If the current grid has different values, set isLeaf to False and set val to any value and divide the current grid into four sub-grids as shown in the photo.
  3. Recurse for each of the children with the proper sub-grid.

If you want to know more about the Quad-Tree, you can refer to the wiki.

Quad-Tree format:

The output represents the serialized format of a Quad-Tree using level order traversal, where null signifies a path terminator where no node exists below.

It is very similar to the serialization of the binary tree. The only difference is that the node is represented as a list [isLeaf, val].

If the value of isLeaf or val is True we represent it as 1 in the list [isLeaf, val] and if the value of isLeaf or val is False we represent it as 0.

Example 1:

Input: grid = [[0,1],[1,0]]
Output: [[0,1],[1,0],[1,1],[1,1],[1,0]]
Explanation: The explanation of this example is shown below:
Notice that 0 represnts False and 1 represents True in the photo representing the Quad-Tree.

Example 2:

Input: grid = [[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1],[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]]
Output: [[0,1],[1,1],[0,1],[1,1],[1,0],null,null,null,null,[1,0],[1,0],[1,1],[1,1]]
Explanation: All values in the grid are not the same. We divide the grid into four sub-grids.
The topLeft, bottomLeft and bottomRight each has the same value.
The topRight have different values so we divide it into 4 sub-grids where each has the same value.
Explanation is shown in the photo below:

Constraints:

  • n == grid.length == grid[i].length
  • n == 2x where 0 <= x <= 6

题解:

When presenting the grid with quad tree.

If the grid has all identical val, then it could be a leaf. Its val is boolean value, when all vals are 1, then node val is 1. vice versa.

Split the grid into 4 parts, if all of them are leaves and all 4 parts value are identical then it is a leaf.

Time Complexity: O(n^2). n = grid.length. The DFS leves level takes n^2,  one level up it takes n^2 / 4, one level up it takes n^2 / 64, the root level takes O(1). Add them up, the sum is smaller than n^2 * 2.

Space: O(logn). stack space.

AC Java:

 1 /*
 2 // Definition for a QuadTree node.
 3 class Node {
 4     public boolean val;
 5     public boolean isLeaf;
 6     public Node topLeft;
 7     public Node topRight;
 8     public Node bottomLeft;
 9     public Node bottomRight;
10 
11     
12     public Node() {
13         this.val = false;
14         this.isLeaf = false;
15         this.topLeft = null;
16         this.topRight = null;
17         this.bottomLeft = null;
18         this.bottomRight = null;
19     }
20     
21     public Node(boolean val, boolean isLeaf) {
22         this.val = val;
23         this.isLeaf = isLeaf;
24         this.topLeft = null;
25         this.topRight = null;
26         this.bottomLeft = null;
27         this.bottomRight = null;
28     }
29     
30     public Node(boolean val, boolean isLeaf, Node topLeft, Node topRight, Node bottomLeft, Node bottomRight) {
31         this.val = val;
32         this.isLeaf = isLeaf;
33         this.topLeft = topLeft;
34         this.topRight = topRight;
35         this.bottomLeft = bottomLeft;
36         this.bottomRight = bottomRight;
37     }
38 };
39 */
40 
41 class Solution {
42     public Node construct(int[][] grid) {
43         if(grid == null || grid.length == 0 || grid[0].length == 0){
44             return null;
45         }
46         
47         return dfs(grid, 0, 0, grid.length);
48     }
49     
50     private Node dfs(int[][] grid, int x, int y, int len){
51         if(len == 1){
52             return new Node(grid[x][y] == 1, true);
53         }
54         
55         Node root = new Node();
56         Node tl = dfs(grid, x, y, len / 2);
57         Node tr = dfs(grid, x, y + len / 2, len / 2);
58         Node bl = dfs(grid, x + len / 2, y, len / 2);
59         Node br = dfs(grid, x + len / 2, y + len / 2, len / 2);
60         if(tl.isLeaf && tr.isLeaf && bl.isLeaf && br.isLeaf && tl.val == tr.val && tr.val == bl.val && bl.val == br.val){
61             root.val = tl.val;
62             root.isLeaf = true;
63         }else{
64             root.topLeft = tl;
65             root.topRight = tr;
66             root.bottomLeft = bl;
67             root.bottomRight = br;
68         }
69         
70         return root;
71     }
72 }

 

posted @ 2022-07-31 14:46  Dylan_Java_NYC  阅读(97)  评论(0编辑  收藏  举报