2Brute2Best
🔥0

Graph Valid Tree

Medium·LeetCode #261 Premium Pro OnlySlight Variation
LeetCode
01

Problem Statement

You have n nodes labelled 0 to n − 1 and a list of undirected edges, where edges[i] = [a, b] is one link between nodes a and b.

Return true if these edges form a valid tree, and false otherwise.

A valid tree is a graph that is connected (you can get from any node to any other node) and has no cycle (no way to leave a node and come back to it without repeating an edge).

Input

n, the number of nodes, and edges, a list of [a, b] pairs.

Output

true if the edges form a tree, otherwise false.

Example 1
Input: n = 5, edges = [[0,1], [0,2], [0,3], [1,4]]
Output: true

Node 0 is the hub: nodes 1, 2 and 3 hang off it, and node 4 hangs off node 1. Five nodes, four edges, and no cycle.

Example 2
Input: n = 5, edges = [[0,1], [1,2], [2,3], [1,3], [1,4]]
Output: false

The links 1–2, 2–3 and 3–1 form a cycle. Every node is connected, but a tree cannot have a cycle.

Constraints
  • • Nodes are labelled 0 to n − 1.
  • • Each edge is a pair [a, b] of node labels from 0 to n − 1.
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

“valid tree”
→ Connected and no cycle, at the same time → count the edges and watch for a failed union.
“undirected edges”
→ Direction does not matter, so one union per edge is enough.
“n nodes and a list of edges”
→ The count n − 1 is a free check before any union happens.
03

Approach Complexity Ladder

Brute Force — BFS for each edge, then one BFS from node 0O(E · (N + E))O(N + E)

Each edge: BFS over the earlier edges to see if its ends are joined; then one BFS to check connectivity

Optimal — count the edges, then unite themO(E · α(N))O(N)

Need exactly n − 1 edges, and no union may fail

Brute Force Intuition

For each edge (a, b), run a BFS from a over the earlier edges only. If b is reached, the edge closes a cycle, so the answer is false. If no edge does, run one BFS from node 0 over all edges: a tree must reach all n nodes.

Recognition rule for this sub-pattern:

Start with n groups; each successful union reduces the count by one.
Remember this · Union-Find (DSU)
Trigger
Edges arrive one by one, "are these connected?", merge equivalent items, count components, extra edge creating a cycle.
Think
Am I repeatedly merging groups and asking whether two things are in the same group?
Key question
What are the elements, and what does it mean to union two of them?
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Union-Find (DSU) translate into production architectures and low-level memory mechanics.