text: class Solution:
      def validTree(self, n: int, edges: List[List[int]]) -> bool:
          par = [i for i in range(n)]
          rank = [1] * n
          
          def find(n):
              p = par[n]
              
              while p != par[p]:
                  p = par[p]
              return p
          
          def union(n1, n2):
              p1, p2 = find(n1), find(n2)
              
              if p1 == p2:
                  return False
              
              if rank[p2] > rank[p1]:
                  par[p1] = p2
                  rank[p2] += rank[p1]
              else:
                  par[p2] = p1
                  rank[p1] += rank[p2]
              return True
                  
          for n1, n2 in edges:
              if not union(n1, n2):
                  return False
          return len(edges) == n - 1
              
          
          