当前位置:网站首页>LeetCode 558. 四叉树交集
LeetCode 558. 四叉树交集
2022-07-17 13:33:00 【Sasakihaise_】




【分治+递归】这道题太长了,但是我们可以简单梳理一下:
1.如果两个节点任何一个为叶子:
如果叶子是1,那么直接返回为1的叶子就行
如果叶子是0,那么直接返回另一个节点
2.递归处理四个孩子,分别得到四个递归后的结果
如果四个都是叶子,
如果都是1,返回一个合并后的大1节点
如果都是0,返回一个合并后的大0节点
否则,返回根据四个递归节点创建一个大节点
/*
// Definition for a QuadTree node.
class Node {
public boolean val;
public boolean isLeaf;
public Node topLeft;
public Node topRight;
public Node bottomLeft;
public Node bottomRight;
public Node() {}
public Node(boolean _val,boolean _isLeaf,Node _topLeft,Node _topRight,Node _bottomLeft,Node _bottomRight) {
val = _val;
isLeaf = _isLeaf;
topLeft = _topLeft;
topRight = _topRight;
bottomLeft = _bottomLeft;
bottomRight = _bottomRight;
}
};
*/
class Solution {
// 4.12 16
public Node dfs(Node n1, Node n2) {
Node node = new Node();
if (n1.isLeaf) {
if (n1.val == true) return new Node(true, true, null, null, null, null);
else return n2;
}
if (n2.isLeaf) {
if (n2.val == true) return new Node(true, true, null, null, null, null);
else return n1;
}
Node tl = dfs(n1.topLeft, n2.topLeft);
Node tr = dfs(n1.topRight, n2.topRight);
Node bl = dfs(n1.bottomLeft, n2.bottomLeft);
Node br = dfs(n1.bottomRight, n2.bottomRight);
if (tl.isLeaf && tr.isLeaf && bl.isLeaf && br.isLeaf) {
if (tl.val && tr.val && bl.val && br.val) {
return new Node(true, true, null, null, null, null);
}
if (tl.val == false && tr.val == false && bl.val == false && br.val == false) {
return new Node(false, true, null, null, null, null);
}
}
return new Node(false, false, tl, tr, bl, br);
}
public Node intersect(Node quadTree1, Node quadTree2) {
return dfs(quadTree1, quadTree2);
}
}
边栏推荐
- 追根问底:Objective-C关联属性原理分析
- [handwritten numeral recognition] handwritten numeral recognition based on lenet network with matlab code
- 【PostgreSQL 】PostgreSQL 15对distinct的优化
- antd 下拉多选传值到后台做查询操作
- 高数__方程与函数的关系
- Thinking about the integrated communication of air, space and earth based on the "7.20 Zhengzhou rainstorm"
- LeetCode 2335. Minimum total time required to fill the cup
- vulnhub inclusiveness: 1
- Establishment of redis cluster, one master, two slave and three Sentinels
- Integrated network architecture and network slicing technology of air, earth and sea
猜你喜欢

英伟达用AI设计GPU:最新H100已经用上,比传统EDA减少25%芯片面积

Avi 部署使用指南(2):Avi 架构概述
![[leetcode weekly replay] 302 weekly 20220717](/img/38/446db9b4755f8b30f9887faede7e95.png)
[leetcode weekly replay] 302 weekly 20220717

发明闪存能赚多少钱?这是一个日本的狗血故事

Common collection properties

Beego framework realizes file upload + seven cattle cloud storage

博弈论(depu)与投资(40/100)

Scala在Idea中的配量

vSphere 下借助 vDS 或 NSX 做端口镜像的方法总结

Redis集群、一主二从三哨兵的搭建
随机推荐
2022/7/14
Aike AI frontier promotion (7.17)
空天地海一体化网络体系架构与网络切片技术
ue4对动画蓝图的理解
Design of the multi live architecture in different places of the king glory mall
Use testeract JS offline recognition picture text record
Establishment of redis cluster, one master, two slave and three Sentinels
腾讯云服务器利用镜像部署WordPress个人网站!
Maximal semi connected subgraph (tarjan contraction + topological ordering + DP longest chain)
Svn learning
vulnhub inclusiveness: 1
leetcode-08
win10开始键点击无响应
使用tesseract.js-offline识别图片文字记录
SSM uses POI to export data to excel
空天地海一体化网络体系架构与网络切片技术
Pytorch手动实现多层感知机
(一)了解MySQL
37. Flex layout
【设计过程】.NET ORM FreeSql WhereDynamicFilter 动态表格查询功能