LeetCode 题解工作台
四叉树交集
二进制矩阵中的所有元素不是 0 就是 1 。 给你两个四叉树, quadTree1 和 quadTree2 。其中 quadTree1 表示一个 n * n 二进制矩阵,而 quadTree2 表示另一个 n * n 二进制矩阵。 请你返回一个表示 n * n 二进制矩阵的四叉树,它是 quadTr…
2
题型
4
代码语言
3
相关题
当前训练重点
中等 · 二分·树·traversal
答案摘要
""" class Node:
Interview AiBoxInterview AiBox 实时 AI 助手,陪你讲清 二分·树·traversal 题型思路
题目描述
二进制矩阵中的所有元素不是 0 就是 1 。
给你两个四叉树,quadTree1 和 quadTree2。其中 quadTree1 表示一个 n * n 二进制矩阵,而 quadTree2 表示另一个 n * n 二进制矩阵。
请你返回一个表示 n * n 二进制矩阵的四叉树,它是 quadTree1 和 quadTree2 所表示的两个二进制矩阵进行 按位逻辑或运算 的结果。
注意,当 isLeaf 为 False 时,你可以把 True 或者 False 赋值给节点,两种值都会被判题机制 接受 。
四叉树数据结构中,每个内部节点只有四个子节点。此外,每个节点都有两个属性:
val:储存叶子结点所代表的区域的值。1 对应 True,0 对应 False;isLeaf: 当这个节点是一个叶子结点时为 True,如果它有 4 个子节点则为 False 。
class Node {
public boolean val;
public boolean isLeaf;
public Node topLeft;
public Node topRight;
public Node bottomLeft;
public Node bottomRight;
}
我们可以按以下步骤为二维区域构建四叉树:
- 如果当前网格的值相同(即,全为
0或者全为1),将isLeaf设为 True ,将val设为网格相应的值,并将四个子节点都设为 Null 然后停止。 - 如果当前网格的值不同,将
isLeaf设为 False, 将val设为任意值,然后如下图所示,将当前网格划分为四个子网格。 - 使用适当的子网格递归每个子节点。

如果你想了解更多关于四叉树的内容,可以参考 百科。
四叉树格式:
输出为使用层序遍历后四叉树的序列化形式,其中 null 表示路径终止符,其下面不存在节点。
它与二叉树的序列化非常相似。唯一的区别是节点以列表形式表示 [isLeaf, val] 。
如果 isLeaf 或者 val 的值为 True ,则表示它在列表 [isLeaf, val] 中的值为 1 ;如果 isLeaf 或者 val 的值为 False ,则表示值为 0 。
示例 1:

输入:quadTree1 = [[0,1],[1,1],[1,1],[1,0],[1,0]] , quadTree2 = [[0,1],[1,1],[0,1],[1,1],[1,0],null,null,null,null,[1,0],[1,0],[1,1],[1,1]] 输出:[[0,0],[1,1],[1,1],[1,1],[1,0]] 解释:quadTree1 和 quadTree2 如上所示。由四叉树所表示的二进制矩阵也已经给出。 如果我们对这两个矩阵进行按位逻辑或运算,则可以得到下面的二进制矩阵,由一个作为结果的四叉树表示。 注意,我们展示的二进制矩阵仅仅是为了更好地说明题意,你无需构造二进制矩阵来获得结果四叉树。![]()
示例 2:
输入:quadTree1 = [[1,0]] , quadTree2 = [[1,0]] 输出:[[1,0]] 解释:两个数所表示的矩阵大小都为 1*1,值全为 0 结果矩阵大小为 1*1,值全为 0 。
提示:
quadTree1和quadTree2都是符合题目要求的四叉树,每个都代表一个n * n的矩阵。n == 2x,其中0 <= x <= 9.
解题思路
方法一
"""
# Definition for a QuadTree node.
class Node:
def __init__(self, val, isLeaf, topLeft, topRight, bottomLeft, bottomRight):
self.val = val
self.isLeaf = isLeaf
self.topLeft = topLeft
self.topRight = topRight
self.bottomLeft = bottomLeft
self.bottomRight = bottomRight
"""
class Solution:
def intersect(self, quadTree1: "Node", quadTree2: "Node") -> "Node":
def dfs(t1, t2):
if t1.isLeaf and t2.isLeaf:
return Node(t1.val or t2.val, True)
if t1.isLeaf:
return t1 if t1.val else t2
if t2.isLeaf:
return t2 if t2.val else t1
res = Node()
res.topLeft = dfs(t1.topLeft, t2.topLeft)
res.topRight = dfs(t1.topRight, t2.topRight)
res.bottomLeft = dfs(t1.bottomLeft, t2.bottomLeft)
res.bottomRight = dfs(t1.bottomRight, t2.bottomRight)
isLeaf = (
res.topLeft.isLeaf
and res.topRight.isLeaf
and res.bottomLeft.isLeaf
and res.bottomRight.isLeaf
)
sameVal = (
res.topLeft.val
== res.topRight.val
== res.bottomLeft.val
== res.bottomRight.val
)
if isLeaf and sameVal:
res = res.topLeft
return res
return dfs(quadTree1, quadTree2)
复杂度分析
| 指标 | 值 |
|---|---|
| 时间 | complexity is O(n^2) in the worst case when every cell is different, because each node may need to be visited once. Space complexity is also O(n^2) due to recursive call stack and potential creation of new Quad-Tree nodes for the result. |
| 空间 | Depends on the final approach |
面试官常问的追问
外企场景- question_mark
Ask clarifying questions about leaf node representation and uniform region compression.
- question_mark
Look for correct recursive traversal and proper handling of OR at leaf nodes.
- question_mark
Expect discussion of time and space trade-offs between expanding every node versus compressing identical leaves.
常见陷阱
外企场景- error
Forgetting to compress four identical child leaves into one parent leaf after recursion.
- error
Incorrectly handling cases where one node is a leaf and the other is not, leading to unnecessary recursion.
- error
Mixing up quadrant order, which produces an incorrect merged Quad-Tree structure.
进阶变体
外企场景- arrow_right_alt
Logical AND of two Quad-Tree binary grids instead of OR.
- arrow_right_alt
Supporting non-square or non-power-of-two matrices with Quad-Tree adaptation.
- arrow_right_alt
Counting the number of true cells in the resulting Quad-Tree without building the tree explicitly.