Reconstruct a Graph into a Tree
Updated
Problem statement
You are given a connected, undirected graph with n vertices numbered from 0 through n - 1, and an array edges of its edges. Rebuild the graph as a tree by keeping only some of its edges. Use this fixed rule to pick them:
- For an edge between
uandv, write it as[min(u, v), max(u, v)], so the smaller vertex comes first. - Sort all edges by their first vertex, and break ties by their second vertex.
- Go through the sorted edges in that order. Keep an edge only if its two ends are not yet connected through edges you already kept. Otherwise skip it, because it would make a cycle.
Return the kept edges in the order you kept them. The result has exactly n - 1 edges, reaches every vertex and has no cycle.
Function
buildCanonicalTree(n: int, edges: int[][]) → int[][]Examples
Example 1
n = 4edges = [[1,3],[0,2],[0,1],[1,2],[2,3]]return = [[0,1],[0,2],[1,3]]After sorting, [0,1] and [0,2] are kept. Edge [1,2] would make a cycle, so it is skipped. Then [1,3] reaches the last vertex, and the tree is complete.
Example 2
n = 5edges = [[3,4],[1,4],[0,3],[1,2],[0,1],[2,3]]return = [[0,1],[0,3],[1,2],[1,4]]After sorting, each of the first four edges joins vertices that are not yet connected, so all four are kept. They already reach every vertex, so each later edge would make a cycle and is skipped.
Constraints
1 <= n <= 200000.0 <= edges.length <= 200000.- Every edge contains two distinct valid vertex IDs.
- The graph is connected and has no duplicate undirected edges.
- For
n = 1,edgesis empty and the answer is empty.
More LinkedIn Onsite Interview questions
- Allocate Mailboxes with Squared DistanceONSITE INTERVIEW · Seen Nov 2026
- Find Leaves of a Binary TreeONSITE INTERVIEW · Seen Nov 2026
- Max Points on a LineONSITE INTERVIEW · Seen Nov 2026
- Explore an Unknown Grid and Find a PathONSITE INTERVIEW · Seen Nov 2026
- Fire Station Response BillONSITE INTERVIEW · Seen Nov 2026
- Roll a Weighted DieONSITE INTERVIEW · Seen Oct 2026
- Longest Binary Subarray With at Most K OnesONSITE INTERVIEW · Seen Oct 2026
- Kth Closest Element in a BSTONSITE INTERVIEW · Seen Oct 2026
- Longest Binary Window with at Most K OnesONSITE INTERVIEW · Seen Oct 2026
- LRU Cache OperationsONSITE INTERVIEW · Seen Oct 2026
Similar questions at other companies
- Earliest Time of Full ConnectionGoogle · ONSITE INTERVIEWGoogle interview questions
- Accounts MergeOracle · ONSITE INTERVIEWOracle interview questions
- Find All People With SecretIDFC Bank · ONSITE INTERVIEWIDFC Bank interview questions
- Group Transitive String AliasesApple · PHONE SCREENApple interview questions
- TikTok Server Network OptimizationTikTok · OATikTok interview questions
- Mutual-Friend Referral RecommendationsFireworks AI · PHONE SCREEN · ONSITE INTERVIEW
- Mutual-Friend RecommendationsMeta · PHONE SCREEN · ONSITE INTERVIEWMeta interview questions
- Evaluate DivisionPlaud · ONSITE INTERVIEW
- Earliest Time All Users Are ConnectedUber · PHONE SCREEN · ONSITE INTERVIEWUber interview questions
- Count Friend CirclesOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions