You have a graph of n nodes labeled from 0 to n - 1. You are given an integer n and a list of edges where edges[i] = [a_i, b_i] indicates that there is an undirected edge between nodes a_i and b_i in the graph. Return true if the edges of the given graph make up a valid tree, and false otherwise.
Example 1
Input: n = 5, edges = [[0,1],[0,2],[0,3],[1,4]]
Output: true
Explanation: There are 4 edges connecting 5 nodes, all reachable from node 0, and no edge closes a loop. A connected graph with n - 1 edges and no cycle is a tree.
Example 2
Input: n = 5, edges = [[0,1],[1,2],[2,3],[1,3],[1,4]]
Output: false
Explanation: The edges 1-2, 2-3 and 1-3 form a cycle, so the graph cannot be a tree.
Example 3
Input: n = 1, edges = []
Output: true
Explanation: A single node with no edges is a valid tree.
A graph is a tree exactly when it is connected and acyclic. With exactly n-1 edges, 'no cycle' already guarantees 'connected', so union-find just has to reject any edge whose endpoints are already joined.
Run checks all cases above. Submit evaluates all test cases.