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. 关于DOM的事件操作

    一.JavaScript的组成 JavaScript基础分为三个部分: ECMAScript:JavaScript的语法标准.包括变量.表达式.运算符.函数.if语句.for语句等. DOM:文档对象 ...

  2. [源码分析]ReentrantLock & AbstractQueuedSynchronizer & Condition

    首先声明一点: 我在分析源码的时候, 把jdk源码复制出来进行中文的注释, 有时还进行编译调试什么的, 为了避免和jdk原生的类混淆, 我在类前面加了"My". 比如把Reentr ...

  3. Angular记录(3)

    文档资料 箭头函数--MDN:https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Functions/Arrow_fun ...

  4. Python高级笔记(三) -- 私有化

    1. 类型1 2. 多模块开发 2.1 import 2.2 from xxx import MyName 区分 import xxx ==> xxx.MyName 可以修改 from xxx ...

  5. Cmake出现CMake Error: Could not find CMAKE_ROOT !!!

    试了很多方式, 其实只需要这一句话!!!!!! hash -r

  6. python 高级部分精华--那些书本不会告诉你的坑

    递归目录生成器方式, else 里的 tmp 显示获取 yield 不可缺少 , 递归算法中若要使用生成器,需要在生成器的原函数(首次调用)显式得到所有yield值 def get_file_recu ...

  7. k8s网络之设计与实现

    k8s网络主题系列: 一.k8s网络之设计与实现 二.k8s网络之Flannel网络 三.k8s网络之Calico网络 K8s网络设计与实现是在学习k8s网络过程中总结的内容.在学习k8s网络各种插件 ...

  8. 网络协议 HTTP入门学习

    概述 Web 的诞生,源于三大技术的诞生,它们都是当年 Web 之父 Tim Berners-Lee 自己 开发的,世界上第一个网站诞生的时间是 1991 年,三大技术的诞生也就是在此之前的不久: 1 ...

  9. BLSTM的训练算法、解码算法以及模型的改进

    摘要 BLSTM解码时,解码器需要等待整个音频到达后才开始解码,因为时间反方向的前向传播需要末尾的历史信息.BLSTM这一延时问题使其不适用与实时语音识别.context-sensitive-chun ...

  10. 【7】学习C++之类的构造函数

    (说实话,我一开始真没觉得构造函数这块有多重要,但是看的视频中老师却花了不少的时间去讲这块内容,本着整理了不亏的心态还是整理了一下) 1.常见的构造函数 C++的类在创建对象的时候,都会去调用构造函数 ...