LeetCode 题解工作台
连接两棵树后最大目标节点数目 II
有两棵 无向 树,分别有 n 和 m 个树节点。两棵树中的节点编号分别为 [0, n - 1] 和 [0, m - 1] 中的整数。 给你两个二维整数 edges1 和 edges2 ,长度分别为 n - 1 和 m - 1 ,其中 edges1[i] = [a i , b i ] 表示第一棵树中节…
3
题型
7
代码语言
3
相关题
当前训练重点
困难 · 二分·树·traversal
答案摘要
节点 的目标节点数可以分成两部分计算: - 第一棵树中与节点 的深度奇偶性相同的节点数;
Interview AiBoxInterview AiBox 实时 AI 助手,陪你讲清 二分·树·traversal 题型思路
题目描述
有两棵 无向 树,分别有 n 和 m 个树节点。两棵树中的节点编号分别为[0, n - 1] 和 [0, m - 1] 中的整数。
给你两个二维整数 edges1 和 edges2 ,长度分别为 n - 1 和 m - 1 ,其中 edges1[i] = [ai, bi] 表示第一棵树中节点 ai 和 bi 之间有一条边,edges2[i] = [ui, vi] 表示第二棵树中节点 ui 和 vi 之间有一条边。
如果节点 u 和节点 v 之间路径的边数是偶数,那么我们称节点 u 是节点 v 的 目标节点 。注意 ,一个节点一定是它自己的 目标节点 。
请你返回一个长度为 n 的整数数组 answer ,answer[i] 表示将第一棵树中的一个节点与第二棵树中的一个节点连接一条边后,第一棵树中节点 i 的 目标节点 数目的 最大值 。
注意 ,每个查询相互独立。意味着进行下一次查询之前,你需要先把刚添加的边给删掉。
示例 1:
输入:edges1 = [[0,1],[0,2],[2,3],[2,4]], edges2 = [[0,1],[0,2],[0,3],[2,7],[1,4],[4,5],[4,6]]
输出:[8,7,7,8,8]
解释:
- 对于
i = 0,连接第一棵树中的节点 0 和第二棵树中的节点 0 。 - 对于
i = 1,连接第一棵树中的节点 1 和第二棵树中的节点 4 。 - 对于
i = 2,连接第一棵树中的节点 2 和第二棵树中的节点 7 。 - 对于
i = 3,连接第一棵树中的节点 3 和第二棵树中的节点 0 。 - 对于
i = 4,连接第一棵树中的节点 4 和第二棵树中的节点 4 。

示例 2:
输入:edges1 = [[0,1],[0,2],[0,3],[0,4]], edges2 = [[0,1],[1,2],[2,3]]
输出:[3,6,6,6,6]
解释:
对于每个 i ,连接第一棵树中的节点 i 和第二棵树中的任意一个节点。

提示:
2 <= n, m <= 105edges1.length == n - 1edges2.length == m - 1edges1[i].length == edges2[i].length == 2edges1[i] = [ai, bi]0 <= ai, bi < nedges2[i] = [ui, vi]0 <= ui, vi < m- 输入保证
edges1和edges2都表示合法的树。
解题思路
方法一:DFS
节点 的目标节点数可以分成两部分计算:
- 第一棵树中与节点 的深度奇偶性相同的节点数;
- 第二棵树中深度奇偶性相同的节点数的最大值。
我们先通过深度优先搜索,计算出第二棵树中深度奇偶性相同的节点数,记为 ,然后再计算第一棵树中与节点 的深度奇偶性相同的节点数,记为 ,那么节点 的目标节点数就是 。
时间复杂度 ,空间复杂度 。其中 和 分别是第一棵树和第二棵树的节点数。
class Solution:
def maxTargetNodes(
self, edges1: List[List[int]], edges2: List[List[int]]
) -> List[int]:
def build(edges: List[List[int]]) -> List[List[int]]:
n = len(edges) + 1
g = [[] for _ in range(n)]
for a, b in edges:
g[a].append(b)
g[b].append(a)
return g
def dfs(
g: List[List[int]], a: int, fa: int, c: List[int], d: int, cnt: List[int]
):
c[a] = d
cnt[d] += 1
for b in g[a]:
if b != fa:
dfs(g, b, a, c, d ^ 1, cnt)
g1 = build(edges1)
g2 = build(edges2)
n, m = len(g1), len(g2)
c1 = [0] * n
c2 = [0] * m
cnt1 = [0, 0]
cnt2 = [0, 0]
dfs(g2, 0, -1, c2, 0, cnt2)
dfs(g1, 0, -1, c1, 0, cnt1)
t = max(cnt2)
return [t + cnt1[c1[i]] for i in range(n)]
复杂度分析
| 指标 | 值 |
|---|---|
| 时间 | O(n + m) |
| 空间 | O(n + m) |
面试官常问的追问
外企场景- question_mark
Check the candidate's ability to traverse trees and compute distances efficiently.
- question_mark
Evaluate their understanding of target node relationships based on even and odd distances.
- question_mark
Assess their ability to optimize the solution using efficient data structures and algorithms.
常见陷阱
外企场景- error
Failing to correctly track the even and odd distances during traversal can lead to incorrect target node calculations.
- error
Not optimizing the solution for large inputs (n and m up to 10^5) may result in a time-out.
- error
Overlooking edge cases where trees are highly imbalanced or when the connection strategy doesn't lead to maximizing the target nodes.
进阶变体
外企场景- arrow_right_alt
Change the problem by considering directed trees instead of undirected trees.
- arrow_right_alt
Consider other types of node relationships, such as distance-based instead of even-odd distance.
- arrow_right_alt
Modify the problem to calculate the number of nodes that are reachable based on a different distance metric (e.g., odd distance).