558. Quad Tree Intersection
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的更多相关文章
- [leetcode_easy]558. Quad Tree Intersection
problem 558. Quad Tree Intersection re 1. Leetcode_easy_558. Quad Tree Intersection; 2. Grandyang; e ...
- 【LeetCode】558. Quad Tree Intersection 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- [LeetCode] Quad Tree Intersection 四叉树相交
A quadtree is a tree data in which each internal node has exactly four children: topLeft, topRight, ...
- LeetCode算法题-Quad Tree Intersection(Java实现)
这是悦乐书的第260次更新,第273篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第127题(顺位题号是558).四叉树是树数据,其中每个内部节点恰好有四个子节点:top ...
- [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 ...
- 【leetcode】427. Construct Quad Tree
problem 427. Construct Quad Tree 参考 1. Leetcode_427. Construct Quad Tree; 完
- [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 ...
- 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 ...
- 2016湖南省赛 I Tree Intersection(线段树合并,树链剖分)
2016湖南省赛 I Tree Intersection(线段树合并,树链剖分) 传送门:https://ac.nowcoder.com/acm/contest/1112/I 题意: 给你一个n个结点 ...
随机推荐
- HTTP与HTTPS(转)
一.什么是HTTP? 什么是HTTPS? HTTP:(Hyper Text Transfer Protocol 超文本传输协议) HTTPS:(Hyper Text Transfer Protoco ...
- 计算指定文件的MD5值
/// <summary> /// 计算指定文件的MD5值 /// </summary> /// <param name="fileName"> ...
- Linux进阶知识和命令
一.Linux目录结构 目录 说明 /lost found系统修复 /bin 二进制命令所在的目录. /boot 系统引导程序所需的文件目录.安装系统分区的时候一般单独要分一个boot分区,大小可谓1 ...
- 浅谈Kubernetes生产架构
注意本文,只是笔者针对Kubernetes生产环境运行的一些关于架构设计和实现方案的总结,内容很粗糙,同时也会不断完善. 首先,我们来梳理下Kubernetes生产架构,其设计适用于绝大多数环境.如下 ...
- 【持续跟新】剑指Offer_Java实现
[第一题 ]二维数组中的查找 package sword_finger_offer; import org.junit.jupiter.api.Test; /** * 剑指offer习题一 二维数组中 ...
- WebDriver下载地址
http://chromedriver.storage.googleapis.com/index.html https://blog.csdn.net/ccggaag/article/details/ ...
- 微信小程序 TLS 版本必须大于等于1.2问题解决
微信小程序 TLS 版本必须大于等于1.2问题解决 此问题最近在微信小程序开发中,比较常见. 在解决这个问题之前,我们需要了解一下,当前的系统环境是否支持TLS1.2以上,可以参考一下表格: 确认系 ...
- 深入剖析Kubernetes学习笔记:深入理解镜像(07)
一.容器里的进程看到的文件系统又是什么样子呢? 1.你会看到好多宿主机的文件 [root@k8s-master ~]# vim ns.c [root@k8s-master ~]# gcc -o nl ...
- CSS3 Background-clip
上一节在<CSS3 background-size>详细的介绍了CSS3为background新增属性之一,今天和大家一起来学习CSS3中有关于Background的第二新属性Backgr ...
- postgresql设置主键
replace(((uuid_generate_v4())::character varying)::text, '-'::text, ''::text)