https://leetcode.com/problems/quad-tree-intersection/description/

我觉得是用意挺好的一题目。求两个四叉树的逻辑union,可惜测试用例里面居然包含对题目外因素的检查(那个id)懒得弄了。

思路其实挺简单,但是很容易忽略一个edge case,就是当所有children 的value 都一致时合并成整个leaf Node。

/*
// Definition for a QuadTree node.
class Node {
public:
bool val;
bool isLeaf;
Node* topLeft;
Node* topRight;
Node* bottomLeft;
Node* bottomRight; Node() {} Node(bool _val, bool _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* intersect(Node* quadTree1, Node* quadTree2) {
if (quadTree1->isLeaf) {
if (quadTree1->val == true) {
return quadTree1;
} else {
return quadTree2;
}
}
else if (quadTree2->isLeaf) {
if (quadTree2->val == true) {
return quadTree2;
} else {
return quadTree1;
}
} Node* topLeft = intersect(quadTree1->topLeft, quadTree2->topLeft);
Node* topRight = intersect(quadTree1->topRight, quadTree2->topRight);
Node* bottomLeft = intersect(quadTree1->bottomLeft, quadTree2->bottomLeft);
Node* bottomRight = intersect(quadTree1->bottomRight, quadTree2->bottomRight); if (topLeft->isLeaf && topRight->isLeaf && bottomLeft->isLeaf && bottomRight->isLeaf) {
if (topLeft->val == topRight->val == bottomLeft->val == bottomRight->val) {
return new Node(topLeft->val, true, nullptr, nullptr, nullptr, nullptr);
}
}
return new Node(, false, topLeft, topRight, bottomLeft, bottomRight);
}
};

558. Quad Tree Intersection的更多相关文章

  1. [leetcode_easy]558. Quad Tree Intersection

    problem 558. Quad Tree Intersection re 1. Leetcode_easy_558. Quad Tree Intersection; 2. Grandyang; e ...

  2. 【LeetCode】558. Quad Tree Intersection 解题报告(Python)

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

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

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

  4. LeetCode算法题-Quad Tree Intersection(Java实现)

    这是悦乐书的第260次更新,第273篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第127题(顺位题号是558).四叉树是树数据,其中每个内部节点恰好有四个子节点:top ...

  5. [LeetCode] 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 ...

  6. 【leetcode】427. Construct Quad Tree

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

  7. [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 ...

  8. 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 ...

  9. 2016湖南省赛 I Tree Intersection(线段树合并,树链剖分)

    2016湖南省赛 I Tree Intersection(线段树合并,树链剖分) 传送门:https://ac.nowcoder.com/acm/contest/1112/I 题意: 给你一个n个结点 ...

随机推荐

  1. 机器学习在入侵检测方面的应用 - 基于ADFA-LD训练集训练入侵检测判别模型

    1. ADFA-LD数据集简介 ADFA-LD数据集是澳大利亚国防学院对外发布的一套主机级入侵检测数据集合,包括Linux和Windows,是一个包含了入侵事件的系统调用syscall序列的数据集(以 ...

  2. jQuery使用():Deferred有状态的回调列表(含源码)

    deferred的功能及其使用 deferred的实现原理及模拟源码 一.deferred的功能及其使用 deferred的底层是基于callbacks实现的,建议再熟悉callbacks的内部机制前 ...

  3. windows的WSl安装mysql数据库以及操作数据库

    1.更新 sudo apt-get update sudo apt-get upgrade 2.安装mysql sudo apt-get install mysql-server 3.开启服务 sud ...

  4. APPLE-SA-2019-3-27-1 watchOS 5.2

    APPLE-SA-2019-3-27-1 watchOS 5.2 watchOS 5.2 is now available and addresses the following: CFStringA ...

  5. HDU-6031 Innumerable Ancestors(二分+树上倍增)

    题意 给一棵树,$m$次询问,每次询问给两个点集问从两个点集中各取一个点的$LCA$的最大深度. 思路 二分答案.对于某个二分过程中得到的$Mid$,如果可行则两个点集在$Mid$所在的深度存在公共的 ...

  6. orcle数据库表中字段值含有单引号,如何模糊搜索?

    例如:T_table表中,name字段值为:字符串含有‘单引号’: SQL模糊搜索语句应该如下:select * from T_table where name like '%含有''单引号''%'

  7. 2018-2019-2 20175306实验二面向对象程序设计《Java开发环境的熟悉》实验报告

    2018-2019-2 20175306实验二面向对象程序设计<Java开发环境的熟悉>实验报告 面向对象程序设计-1 实验要求: 参考:> http://www.cnblogs.c ...

  8. h5页面使用js实现保存当前图片到手机相册

    很可惜,这个鬼东西微信内置浏览器不适用 页面: <!doctype html> <html> <head> <meta charset="UTF-8 ...

  9. tensorflow 莫烦教程

    1,感谢莫烦 2,第一个实例:用tf拟合线性函数 import tensorflow as tf import numpy as np # create data x_data = np.random ...

  10. 软件测试之实际工作工作方式001--log4

    软件测试之实际工作工作方式001--log4 Dotest软件测试-董浩整理   领导安排任务后:   1)首先要确认理解:是指的某工作吗?具体有什么要求吗?时间截止到什么时候? 解析:   a.万一 ...