Skip to main content
Back to problems
Leetcode
Medium
Trees
Graphs
Graph Connectivity
Google
Meta
Maximize The Number Of Target Nodes After Connecting Trees I

Given two trees and an integer kk, connect one node from the first tree to one node from the second tree. For each node in the first tree, count how many nodes in the combined graph are within distance at most kk after the connection, and maximize that count.

Acceptance 0%
Problem Statement

Problem

You are given two undirected trees, tree1 and tree2, with n and m nodes respectively. Each tree is described by its edges.

You may add exactly one edge connecting any node in tree1 to any node in tree2.

For a chosen node u in tree1, define its target nodes as the nodes whose distance from u in the new combined graph is at most k.

Return an array where the i-th value is the maximum possible number of target nodes for node i in tree1 after choosing the best connection edge between the two trees.

Key idea

The connection between the trees changes distances only through the newly added bridge. The task is to count nodes reachable within a distance limit and maximize the contribution from the second tree by placing the bridge strategically.

Input Format

  • edges1: edges of the first tree, where edges1.length = n - 1
  • edges2: edges of the second tree, where edges2.length = m - 1
  • k: non-negative integer distance limit

Each edge is an undirected pair of nodes using 0-based labels.

Output Format

  • Return an integer array ans of length n
  • ans[i] is the maximum number of nodes with distance <= k from node i in tree1 after connecting the two trees with one edge

Constraints

  • Both tree1 and tree2 are valid trees.
  • Node labels are distinct within each tree and start at 0.
  • The added edge must connect one node from tree1 to one node from tree2.
  • 0k0 \le k
  • The exact input sizes are not provided in the source metadata.
Examples
Sample cases returned by the problem API.

Example 1

Input

edges1 = [[0,1],[1,2],[1,3]], edges2 = [[0,1],[0,2],[2,3]], k = 2

Output

[6,6,6,5]

Explanation

For each node in tree1, count nodes within distance 2 after choosing the best edge between the trees.

  • Node 0 can reach nodes 0,1,2,3 in tree1 and 2 nodes in tree2 through a good connection, for a total of 6.
  • Node 1 can reach all 4 nodes in tree1 and 2 nodes in tree2, for a total of 6.
  • Node 2 can also reach 4 nodes in tree1 and 2 nodes in tree2, for a total of 6.
  • Node 3 can reach 3 nodes in tree1 and 2 nodes in tree2, for a total of 5.

Example 2

Input

edges1 = [[0,1]], edges2 = [[0,1]], k = 1

Output

[3,3]

Explanation

Each node in tree1 can always count itself and its direct neighbor in tree1. By connecting the trees through either endpoint, one node from tree2 becomes reachable within distance 1 from the chosen endpoint in tree1, giving a total of 3 for each node.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.