题目

给你一个 n * n 矩阵 grid ,矩阵由若干 0 和 1 组成。请你用四叉树表示该矩阵 grid 。

你需要返回能表示矩阵的 四叉树 的根结点。

注意,当 isLeaf 为 False 时,你可以把 True 或者 False 赋值给节点,两种值都会被判题机制 接受 。

四叉树数据结构中,每个内部节点只有四个子节点。此外,每个节点都有两个属性:

val:储存叶子结点所代表的区域的值。1 对应 True,0 对应 False;
isLeaf: 当这个节点是一个叶子结点时为 True,如果它有 4 个子节点则为 False 。

class Node {
public boolean val;
public boolean isLeaf;
public Node topLeft;
public Node topRight;
public Node bottomLeft;
public Node bottomRight;
}

我们可以按以下步骤为二维区域构建四叉树:

如果当前网格的值相同(即,全为 0 或者全为 1),将 isLeaf 设为 True ,将 val 设为网格相应的值,并将四个子节点都设为 Null 然后停止。
如果当前网格的值不同,将 isLeaf 设为 False, 将 val 设为任意值,然后如下图所示,将当前网格划分为四个子网格。
使用适当的子网格递归每个子节点。 image.png

如果你想了解更多关于四叉树的内容,可以参考 wiki 。

四叉树格式:

输出为使用层序遍历后四叉树的序列化形式,其中 null 表示路径终止符,其下面不存在节点。

它与二叉树的序列化非常相似。唯一的区别是节点以列表形式表示 [isLeaf, val] 。

如果 isLeaf 或者 val 的值为 True ,则表示它在列表 [isLeaf, val] 中的值为 1 ;如果 isLeaf 或者 val 的值为 False ,则表示值为 0 。

示例 1: image.png

输入:grid = [[0,1],[1,0]]
输出:[[0,1],[1,0],[1,1],[1,1],[1,0]]
解释:此示例的解释如下:
请注意,在下面四叉树的图示中,0 表示 false,1 表示 True 。 image.png

示例 2: image.png

输入: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]]
输出:[[0,1],[1,1],[0,1],[1,1],[1,0],null,null,null,null,[1,0],[1,0],[1,1],[1,1]]
解释:网格中的所有值都不相同。我们将网格划分为四个子网格。
topLeft,bottomLeft 和 bottomRight 均具有相同的值。
topRight 具有不同的值,因此我们将其再分为 4 个子网格,这样每个子网格都具有相同的值。
解释如下图所示: image.png

示例 3:

输入:grid = [[1,1],[1,1]]
输出:[[1,1]]
示例 4:

输入:grid = [[0]]
输出:[[1,0]]
示例 5:

输入:grid = [[1,1,0,0],[1,1,0,0],[0,0,1,1],[0,0,1,1]]
输出:[[0,1],[1,1],[1,0],[1,0],[1,1]]

提示:

n == grid.length == grid[i].length
n == 2^x 其中 0 <= x <= 6

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/construct-quad-tree
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

思路

理解题意是关键,将矩阵分为四个相等的块,如果一个块内值完全一样就是一个叶子结点,不再递归;如果不一样,就继续划分四个块,继续判断,由此形成了递归。递归函数传递矩阵块的左上和右下顶点的坐标。

代码

  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. public Node() {
  11. this.val = false;
  12. this.isLeaf = false;
  13. this.topLeft = null;
  14. this.topRight = null;
  15. this.bottomLeft = null;
  16. this.bottomRight = null;
  17. }
  18. public Node(boolean val, boolean isLeaf) {
  19. this.val = val;
  20. this.isLeaf = isLeaf;
  21. this.topLeft = null;
  22. this.topRight = null;
  23. this.bottomLeft = null;
  24. this.bottomRight = null;
  25. }
  26. public Node(boolean val, boolean isLeaf, Node topLeft, Node topRight, Node bottomLeft, Node bottomRight) {
  27. this.val = val;
  28. this.isLeaf = isLeaf;
  29. this.topLeft = topLeft;
  30. this.topRight = topRight;
  31. this.bottomLeft = bottomLeft;
  32. this.bottomRight = bottomRight;
  33. }
  34. };
  35. */
  36. class Solution {
  37. public Node construct(int[][] grid) {
  38. return dfs(grid, 0, 0, grid.length - 1, grid.length - 1);
  39. }
  40. public Node dfs(int[][] grid, int r0, int c0, int r1, int c1) {
  41. boolean flag = true;
  42. for (int i = r0; i <= r1; ++i) {
  43. for (int j = c0; j <= c1; ++j) {
  44. if (grid[i][j] != grid[r0][c0]) {
  45. flag = false;
  46. break;
  47. }
  48. }
  49. if (!flag) {
  50. break;
  51. }
  52. }
  53. if (flag) {
  54. return new Node(grid[r0][c0] == 1, true);
  55. } else {
  56. return new Node(
  57. true,
  58. false,
  59. dfs(grid, r0, c0, (r0 + r1) / 2, (c0 + c1) / 2),
  60. dfs(grid, r0, (c0 + c1) / 2 + 1, (r0 + r1) / 2, c1),
  61. dfs(grid, (r0 + r1) / 2 + 1, c0, r1, (c0 + c1) / 2),
  62. dfs(grid, (r0 + r1) / 2 + 1, (c0 + c1) / 2 + 1, r1, c1)
  63. );
  64. }
  65. }
  66. }

更好的写法是像官解这样,右下角的坐标大1,大1始终是偶数,除以2一定可以整除,就不用考虑要不要在参数中加1了。很赞。

  1. class Solution {
  2. public Node construct(int[][] grid) {
  3. return dfs(grid, 0, 0, grid.length, grid.length);
  4. }
  5. public Node dfs(int[][] grid, int r0, int c0, int r1, int c1) {
  6. boolean same = true;
  7. for (int i = r0; i < r1; ++i) {
  8. for (int j = c0; j < c1; ++j) {
  9. if (grid[i][j] != grid[r0][c0]) {
  10. same = false;
  11. break;
  12. }
  13. }
  14. if (!same) {
  15. break;
  16. }
  17. }
  18. if (same) {
  19. return new Node(grid[r0][c0] == 1, true);
  20. }
  21. Node ret = new Node(
  22. true,
  23. false,
  24. dfs(grid, r0, c0, (r0 + r1) / 2, (c0 + c1) / 2),
  25. dfs(grid, r0, (c0 + c1) / 2, (r0 + r1) / 2, c1),
  26. dfs(grid, (r0 + r1) / 2, c0, r1, (c0 + c1) / 2),
  27. dfs(grid, (r0 + r1) / 2, (c0 + c1) / 2, r1, c1)
  28. );
  29. return ret;
  30. }
  31. }