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 valisLeaf 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:

  1. N is less than 1000 and guaranteened to be a power of 2.
  2. If you want to know more about the quad tree, you can refer to its wiki.

这道题让我们根据一个二维数组来建立一棵四叉树,关于四叉树的介绍题目中也了给了wiki百科的链接。但是博主开始看到题目中给的那个例子,没怎么看懂。后来分析大神们的代码,才略微弄明白了一些。原来叶结点表示的是值相同的一片区域,比如我们看二维数组图示那行的第三个图,首先整个数组被分成了四等份,左上,左下,和右下部分内的值均相同,那么他们都是一个叶结点,而右上只有再四等分一下,才能使各自部分内的值相同,所以其就不是叶结点,而四等分后的每个区间才是叶结点。题目中限定了N的值一定是2的指数,就是说其如果可分的话,一定可以四等分,而之前说了,只有区间内的值不同时,才需要四等分,否则整体就当作一个叶结点。所以我们需要check四等分区间内的值是否相同,当然,我们可以将二维数组拆分为四个二维数组,但是那样可能不太高效,而且还占用额外空间,一个比较好的选择是用坐标变量来控制等分数组的范围,我们只需要一个起始点坐标,和区间的长度,就可以精确定位一个区间了。比如说对于例子中的整个二维数组数组来说,知道起始点坐标 (0, 0),还有长度8,就知道表示的是哪个区间。我们可以遍历这个区间上的其他所有的点,跟起点对比,只要有任何点跟起点不相同,则说明该区间是可分的,因为我们前面说了,只有一个区间上所有的值均相同,才能当作一个叶结点。只要有不同,就表示可以四分,那么我们就新建一个结点,这里的左上,左下,右上,和右下四个子结点就需要用过调用递归函数来实现了,实现原理都一样,重要的地方就是确定每个四分区间的起点和长度,长度好确定,都是当前长度的一半,然后就是把各个区间的起点确定好,这个也不难,就是细心一点,不要写错了就可以了,另外,对于非叶结点,结点值可以是true或者false都没问题。如果某个区间上所有值均相同,那么就生成一个叶结点,结点值就跟区间值相同,isLeaf是true,四个子结点均为NULL即可,参见代码如下:

解法一:

class Solution {
public:
Node* construct(vector<vector<int>>& grid) {
return build(grid, , , grid.size());
}
Node* build(vector<vector<int>>& grid, int x, int y, int len) {
if (len <= ) return NULL;
for (int i = x; i < x + len; ++i) {
for (int j = y; j < y + len; ++j) {
if (grid[i][j] != grid[x][y]) {
return new Node(true, false,
build(grid, x, y, len / ),
build(grid, x, y + len / , len / ),
build(grid, x + len/ , y, len / ),
build(grid, x + len / , y + len / , len / ));
}
}
}
return new Node(grid[x][y] == , true, NULL, NULL, NULL, NULL);
}
};

还有一种写法,记录了区间的左上点和右下点,知道这两个点也可以确定一个区间的位置,整体思路和上面的方法并没有什么太大的区别,参见代码如下:

解法二:

class Solution {
public:
Node* construct(vector<vector<int>>& grid) {
return build(grid, , , grid.size() - , grid.size() - );
}
Node* build(vector<vector<int>>& grid, int r1, int c1, int r2, int c2) {
if (r1 > r2 || c1 > c2) return NULL;
bool isLeaf = true;
int val = grid[r1][c1], rowMid = (r1 + r2) / , colMid = (c1 + c2) / ;
for (int i = r1; i <= r2; ++i) {
for (int j = c1; j <= c2; ++j) {
if (grid[i][j] != val) {
isLeaf = false;
break;
}
}
}
if (isLeaf) return new Node(val == , true, NULL, NULL, NULL, NULL);
return new Node(false, false,
build(grid, r1, c1, rowMid, colMid),
build(grid, r1, colMid + , rowMid, c2),
build(grid, rowMid + , c1, r2, colMid),
build(grid, rowMid + , colMid + , r2, c2));
}
};

参考资料:

https://leetcode.com/problems/construct-quad-tree/

https://leetcode.com/problems/construct-quad-tree/discuss/151j684/Recursive-Java-Solution

https://leetcode.com/problems/construct-quad-tree/discuss/154420/My-Java-Recursive-Solution

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Construct Quad Tree 建立四叉树的更多相关文章

  1. [LeetCode] Quad Tree Intersection 四叉树相交

    A quadtree is a tree data in which each internal node has exactly four children: topLeft, topRight,  ...

  2. 【leetcode】427. Construct Quad Tree

    problem 427. Construct Quad Tree 参考 1. Leetcode_427. Construct Quad Tree; 完

  3. LeetCode:Construct Binary Tree from Inorder and Postorder Traversal,Construct Binary Tree from Preorder and Inorder Traversal

    LeetCode:Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder trav ...

  4. [LeetCode] Construct Binary Tree from Preorder and Inorder Traversal 由先序和中序遍历建立二叉树

    Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...

  5. 【LeetCode】427. Construct Quad Tree 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  6. [LeetCode&Python] Problem 427. Construct Quad Tree

    We want to use quad trees to store an N x N boolean grid. Each cell in the grid can only be true or ...

  7. leetcode 427. Construct Quad Tree

    We want to use quad trees to store an N x N boolean grid. Each cell in the grid can only be true or ...

  8. Leetcode, construct binary tree from inorder and post order traversal

    Sept. 13, 2015 Spent more than a few hours to work on the leetcode problem, and my favorite blogs ab ...

  9. LeetCode: Construct Binary Tree from Inorder and Postorder Traversal 解题报告

    Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...

随机推荐

  1. 点评cat系列-简介

    面上有很多优秀的 OS 级监控系统 (比如 falcon), 这些监控系统主要聚焦在 CPU/IO/Mem/Disk 和应用端口, falcon 甚至可以监控到 JVM. 但对于应用系统内部的一些监控 ...

  2. MVC控制器返回一个list 视图接收

    控制器 public ActionResult InfoFrame() { List<Users> list = new List<Users>(); if (Session[ ...

  3. DensePose: Dense Human Pose Estimation In The Wild(理解)

    0 - 背景 Facebook AI Research(FAIR)开源了一项将2D的RGB图像的所有人体像素实时映射到3D模型的技术(DensePose).支持户外和穿着宽松衣服的对象识别,支持多人同 ...

  4. 208道面试题(JVM部分暂无答案)

    这是从网上看到的一套java面试题, 答案只是一个大概, 另外题目质量参差不齐, 斟酌参考(JVM的部分暂时没有答案) 一.Java 基础 JDK 和 JRE 有什么区别? 答: JDK(Java D ...

  5. 【原创】大叔经验分享(5)oozie提交spark任务如何添加依赖

    spark任务添加依赖的方式: 1 如果是local方式运行,可以通过--jars来添加依赖: 2 如果是yarn方式运行,可以通过spark.yarn.jars来添加依赖: 这两种方式在oozie上 ...

  6. 【原创】大叔问题定位分享(11)Spark中对大表子查询加limit为什么会报Broadcast超时错误

    当两个表需要join时,如果一个是大表,一个是小表,正常的map-reduce流程需要shuffle,这会导致大表数据在节点间网络传输,常见的优化方式是将小表读到内存中并广播到大表处理,避免shuff ...

  7. redis 资料

    redis是什么: Redis is an open source, BSD licensed, advanced key-value store. It is often referred to a ...

  8. Atcoder Grand Contest 032

    打的第一场Atcoder,已经知道会被虐得很惨,但没有想到竟然只做出一题-- 思维急需提升. A - Limited Insertion 这题还是很签到的. 感觉正着做不好做,我们反着来,把加数变为删 ...

  9. Java框架之spring框架的优点,为什么要学习spring框架

    为什么要学习Spring的框架a: 方便解耦,简化开发    Spring就是一个大工厂,可以将所有对象创建和依赖关系维护,交给Spring管理 b:AOP编程的支持      Spring提供面向切 ...

  10. win10安装mysql5.7.20解压版

    mysql安装包可到官网下载,地址:https://dev.mysql.com/downloads/mysql 1.首先解压文件包,我这解压到E:\install_work\mysql目录下: 2.发 ...