Tree = connected + acyclic. Tree has exactly n-1 edges. Use Union-Find: if edge connects same root → cycle → not tree.
n=5, edges=[[0,1],[0,2],[0,3],[3,4]]trueTree: connected + acyclic. n nodes need n-1 edges to connect. More → cycle. Less → disconnected.
You could run a search from every node to look for cycles and check connectivity separately. There is a much simpler test.
A tree with n nodes always has exactly n −1 edges. So if the edge count is not n −1, it cannot be a tree. With exactly n −1 edges, it is a tree if and only if there is no cycle. Use union find: for each edge, if both ends already have the same root, that edge closes a cycle. Otherwise, merge the two groups.
Tree means n −1 edges and no cycle. Union find detects a cycle the moment an edge joins a group to itself.
Run the animation above on your own input, or watch the 1:55 video explanation of Is Graph a Tree? with a dry run and Java code.