FunctionRacerProblemsRace

Forms a Tree

EasyGraphsPython, JavaScript, TypeScript, Java, C++

A graph has n nodes numbered 0 to n - 1 and an undirected edge list edges of pairs [a, b] with a != b. The same pair may be listed twice; a repeated pair counts as two different edges between the same nodes, which together form a cycle.

Return true if the edges form a tree: every node can reach every other node, and there is no cycle. Otherwise return false. A single node with no edges is a tree.

Example 1

n 4
edges [[0,1],[1,2],[1,3]]
returns true

Three edges connect all four nodes without a cycle.

Example 2

n 4
edges [[0,1],[1,2],[2,0]]
returns false

Nodes 0, 1 and 2 form a cycle and node 3 is cut off.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Graphs problems