Magicsheet logo

Maximize the Number of Target Nodes After Connecting Trees II

Hard
100%
Updated 6/1/2025

Maximize the Number of Target Nodes After Connecting Trees II

What is this problem about?

This is a harder variation of connecting two trees. You are given two trees. You can add one edge connecting any node from Tree 1 to any node from Tree 2. You need to return an array for Tree 1, where the ii-th element is the maximum number of target nodes reachable from node ii. In this version, a node is a "target node" if the path from node ii to that node has an even number of edges.

Why is this asked in interviews?

This problem introduces Bipartite Graphs / Tree Coloring. Because the target condition relies on "even path lengths", it elegantly tests a candidate's understanding of graph parity. Interviewers use it to see if you can recognize that any tree is inherently bipartite, and that nodes can be painted in two alternating colors (e.g., Red and Blue) to instantly solve even/odd distance queries in O(1)O(1) time.

Algorithmic pattern used

This problem leverages Tree Bipartition (2-Coloring).

  1. Color Tree 1: Run a DFS. Root is Red. Children are Blue. Grandchildren are Red. Count total Reds and total Blues.
  2. Color Tree 2: Do the exact same thing. Find the max of (Total Reds in Tree 2, Total Blues in Tree 2). Let this be max_tree2_even.
  3. An even path from node i in Tree 1 will reach all nodes in Tree 1 that share the same color as node i.
  4. If we connect node i to Tree 2, the edge itself adds 1 to the path length. To hit nodes in Tree 2 with an even path, we want the connection to act as an alternating bridge. We simply add max_tree2_even to our count.
  5. Answer for node i = (Count of nodes in Tree 1 with same color as i) + max_tree2_even.

Example explanation

Tree 1: A - B - C. Coloring: A (Red), B (Blue), C (Red). Tree 1 totals: 2 Red, 1 Blue. Tree 2: X - Y. Coloring: X (Red), Y (Blue). Tree 2 totals: 1 Red, 1 Blue. Max even reach in Tree 2 is 1.

Query for node A (Red):

  • Even length paths in Tree 1 reach other Red nodes: A and C. (Total 2).
  • We connect A to the best node in Tree 2. We add the max_tree2_even (1).
  • Total for A = 2+1=32 + 1 = 3.

Query for node B (Blue):

  • Even length paths in Tree 1 reach other Blue nodes: B. (Total 1).
  • Connect to Tree 2. Add 1.
  • Total for B = 1+1=21 + 1 = 2.

Common mistakes candidates make

Candidates who don't recognize the bipartite nature of trees will attempt to run BFS from every node to count path lengths, resulting in O(N2)O(N^2) time. The realization that "even distance" simply means "same parity level" or "same color in a bipartite graph" is crucial. Another mistake is forgetting that connecting the edge shifts the parity for Tree 2, but since we just take the max(color1, color2) of Tree 2, it handles itself perfectly.

Interview preparation tip

When tackling graph problems involving "even" or "odd" edge distances, immediately think of Graph Coloring (Bipartite graphs). In a tree, the unique path between two nodes of the same color always has an even length, and between different colors always has an odd length. This property reduces complex distance traversals into simple integer counting.

Similar Questions