Welcome to Subscribe On Youtube
Formatted question description: https://leetcode.ca/all/427.html
427. Construct Quad Tree (Easy)
We want to use quad trees to store an N x N
boolean grid. Each cell in the grid can only be true or false. The root node represents the whole grid. For each node, it will be subdivided into four children nodes until the values in the region it represents are all the same.
Each node has another two boolean attributes : isLeaf
and val
. isLeaf
is true if and only if the node is a leaf node. The val
attribute for a leaf node contains the value of the region it represents.
Your task is to use a quad tree to represent a given grid. The following example may help you understand the problem better:
Given the 8 x 8
grid below, we want to construct the corresponding quad tree:
It can be divided according to the definition above:
The corresponding quad tree should be as following, where each node is represented as a (isLeaf, val)
pair.
For the non-leaf nodes, val
can be arbitrary, so it is represented as *
.
Note:
N
is less than1000
and guaranteened to be a power of 2.- If you want to know more about the quad tree, you can refer to its wiki.
Companies:
Uber
Solution 1.
-
/* // Definition for a QuadTree node. class Node { public boolean val; public boolean isLeaf; public Node topLeft; public Node topRight; public Node bottomLeft; public Node bottomRight; public Node() {} public Node(boolean _val,boolean _isLeaf,Node _topLeft,Node _topRight,Node _bottomLeft,Node _bottomRight) { val = _val; isLeaf = _isLeaf; topLeft = _topLeft; topRight = _topRight; bottomLeft = _bottomLeft; bottomRight = _bottomRight; } }; */ class Solution { public Node construct(int[][] grid) { if (grid == null || grid.length == 0 || grid[0].length == 0) return null; int length = grid.length; if (length == 1) { int gridVal = grid[0][0]; boolean val = gridVal == 1 ? true : false; Node root = new Node(val, true, null, null, null, null); return root; } else { int halfLength = length / 2; int[][] topLeftGrid = new int[halfLength][halfLength]; int[][] topRightGrid = new int[halfLength][halfLength]; int[][] bottomLeftGrid = new int[halfLength][halfLength]; int[][] bottomRightGrid = new int[halfLength][halfLength]; for (int i = 0; i < halfLength; i++) { for (int j = 0; j < halfLength; j++) { topLeftGrid[i][j] = grid[i][j]; topRightGrid[i][j] = grid[i][j + halfLength]; bottomLeftGrid[i][j] = grid[i + halfLength][j]; bottomRightGrid[i][j] = grid[i + halfLength][j + halfLength]; } } Node topLeft = construct(topLeftGrid); Node topRight = construct(topRightGrid); Node bottomLeft = construct(bottomLeftGrid); Node bottomRight = construct(bottomRightGrid); if (topLeft.isLeaf && topRight.isLeaf && bottomLeft.isLeaf && bottomRight.isLeaf && topLeft.val == topRight.val && topLeft.val == bottomLeft.val && topLeft.val == bottomRight.val) { Node root = new Node(topLeft.val, true, null, null, null, null); return root; } else { boolean val = topLeft.val || topRight.val || bottomLeft.val || bottomRight.val; Node root = new Node(val, false, topLeft, topRight, bottomLeft, bottomRight); return root; } } } } ############ /* // Definition for a QuadTree node. class Node { public boolean val; public boolean isLeaf; public Node topLeft; public Node topRight; public Node bottomLeft; public Node bottomRight; public Node() { this.val = false; this.isLeaf = false; this.topLeft = null; this.topRight = null; this.bottomLeft = null; this.bottomRight = null; } public Node(boolean val, boolean isLeaf) { this.val = val; this.isLeaf = isLeaf; this.topLeft = null; this.topRight = null; this.bottomLeft = null; this.bottomRight = null; } public Node(boolean val, boolean isLeaf, Node topLeft, Node topRight, Node bottomLeft, Node bottomRight) { this.val = val; this.isLeaf = isLeaf; this.topLeft = topLeft; this.topRight = topRight; this.bottomLeft = bottomLeft; this.bottomRight = bottomRight; } }; */ class Solution { public Node construct(int[][] grid) { return dfs(0, 0, grid.length - 1, grid[0].length - 1, grid); } private Node dfs(int a, int b, int c, int d, int[][] grid) { int zero = 0, one = 0; for (int i = a; i <= c; ++i) { for (int j = b; j <= d; ++j) { if (grid[i][j] == 0) { zero = 1; } else { one = 1; } } } boolean isLeaf = zero + one == 1; boolean val = isLeaf && one == 1; Node node = new Node(val, isLeaf); if (isLeaf) { return node; } node.topLeft = dfs(a, b, (a + c) / 2, (b + d) / 2, grid); node.topRight = dfs(a, (b + d) / 2 + 1, (a + c) / 2, d, grid); node.bottomLeft = dfs((a + c) / 2 + 1, b, c, (b + d) / 2, grid); node.bottomRight = dfs((a + c) / 2 + 1, (b + d) / 2 + 1, c, d, grid); return node; } }
-
// OJ: https://leetcode.com/problems/construct-quad-tree/ // Time: O(log_4^N * N^2) // Space: O(log_4^N) class Solution { private: Node *dfs(vector<vector<int>> &grid, int x, int y, int N) { bool same = true; for (int i = 0; i < N && same; ++i) { for (int j = 0; j < N && same; ++j) { same = grid[x][y] == grid[x + i][y + j]; } } if (same) return new Node(grid[x][y], true); return new Node(true, false, dfs(grid, x, y, N / 2), dfs(grid, x, y + N / 2, N / 2), dfs(grid, x + N / 2, y, N / 2), dfs(grid, x + N / 2, y + N / 2, N / 2)); } public: Node* construct(vector<vector<int>>& grid) { return dfs(grid, 0, 0, grid.size()); } };
-
""" # Definition for a QuadTree node. class Node: def __init__(self, val, isLeaf, topLeft, topRight, bottomLeft, bottomRight): self.val = val self.isLeaf = isLeaf self.topLeft = topLeft self.topRight = topRight self.bottomLeft = bottomLeft self.bottomRight = bottomRight """ class Solution: def construct(self, grid: List[List[int]]) -> 'Node': def dfs(a, b, c, d): zero = one = 0 for i in range(a, c + 1): for j in range(b, d + 1): if grid[i][j] == 0: zero = 1 else: one = 1 isLeaf = zero + one == 1 val = isLeaf and one if isLeaf: return Node(grid[a][b], True) topLeft = dfs(a, b, (a + c) // 2, (b + d) // 2) topRight = dfs(a, (b + d) // 2 + 1, (a + c) // 2, d) bottomLeft = dfs((a + c) // 2 + 1, b, c, (b + d) // 2) bottomRight = dfs((a + c) // 2 + 1, (b + d) // 2 + 1, c, d) return Node(val, isLeaf, topLeft, topRight, bottomLeft, bottomRight) return dfs(0, 0, len(grid) - 1, len(grid[0]) - 1) ############ """ # Definition for a QuadTree node. class Node: def __init__(self, val, isLeaf, topLeft, topRight, bottomLeft, bottomRight): self.val = val self.isLeaf = isLeaf self.topLeft = topLeft self.topRight = topRight self.bottomLeft = bottomLeft self.bottomRight = bottomRight """ class Solution: def construct(self, grid): """ :type grid: List[List[int]] :rtype: Node """ isLeaf = self.isQuadTree(grid) _len = len(grid) if isLeaf == None: mid = _len // 2 topLeftGrid = [[grid[i][j] for j in range(mid)] for i in range(mid)] topRightGrid = [[grid[i][j] for j in range(mid, _len)] for i in range(mid)] bottomLeftGrid = [[grid[i][j] for j in range(mid)] for i in range(mid, _len)] bottomRightGrid = [[grid[i][j] for j in range(mid, _len)] for i in range(mid, _len)] node = Node(True, False, self.construct(topLeftGrid), self.construct(topRightGrid), self.construct(bottomLeftGrid), self.construct(bottomRightGrid)) elif isLeaf == False: node = Node(False, True, None, None, None, None) else: node = Node(True, True, None, None, None, None) return node def isQuadTree(self, grid): _len = len(grid) _sum = 0 for i in range(_len): _sum += sum(grid[i]) if _sum == _len ** 2: return True elif _sum == 0: return False else: return None
-
/** * Definition for a QuadTree node. * type Node struct { * Val bool * IsLeaf bool * TopLeft *Node * TopRight *Node * BottomLeft *Node * BottomRight *Node * } */ func construct(grid [][]int) *Node { var dfs func(a, b, c, d int) *Node dfs = func(a, b, c, d int) *Node { zero, one := 0, 0 for i := a; i <= c; i++ { for j := b; j <= d; j++ { if grid[i][j] == 0 { zero = 1 } else { one = 1 } } } isLeaf := zero+one == 1 val := isLeaf && one == 1 node := &Node{Val: val, IsLeaf: isLeaf} if isLeaf { return node } node.TopLeft = dfs(a, b, (a+c)/2, (b+d)/2) node.TopRight = dfs(a, (b+d)/2+1, (a+c)/2, d) node.BottomLeft = dfs((a+c)/2+1, b, c, (b+d)/2) node.BottomRight = dfs((a+c)/2+1, (b+d)/2+1, c, d) return node } return dfs(0, 0, len(grid)-1, len(grid[0])-1) }