Solution 1: Union-Found

    private int[] root;
    private int count;
    public boolean validTree(int n, int[][] edges) {
        count = n;
        if(edges.length!=n-1)
            return false;
        root = new int[n];
        for(int i=0;i<n;i++){
            root[i]=i;
        }
        for(int[] edge : edges){
            if(!union(edges, edge[0], edge[1]))
                return false;   //there are circlesl
        }
        return count == 1;
    }
    
    private boolean union(int[][] edges, int x, int y){
        x = find(x);
        y = find(y);
        if(x==y)
            return false;
        root[x]=y;
        count--;     //two points are unioned.
        return true;
    }
    
    private int find(int x){
        if(x==root[x])
            return x;
        else{
            root[x]=find(root[x]);
            return root[x];
        }    
    }

Solution 2: DFS

    Map<Integer, Set<Integer>> map = new HashMap<>();
    boolean[] visited;
    public boolean validTree(int n, int[][] edges) {
        if(edges.length!=n-1)
            return false;
        visited = new boolean[n];
        for(int[] edge: edges){
            map.putIfAbsent(edge[0], new HashSet<>());
            map.putIfAbsent(edge[1], new HashSet<>());
            map.get(edge[0]).add(edge[1]);
            map.get(edge[1]).add(edge[0]);
        }
        visited[0]=true;
        dfs(0, map);
        for(int i=0;i<visited.length;i++)
        {
            if(!visited[i])    //there should be circle
                return false;
        }
        return true;
    }
    
    private void dfs(int node, Map<Integer, Set<Integer>> map){
        if(map.get(node)==null)
            return;
        Set<Integer> children = map.get(node);
        for(int i: children){
            if(!visited[i]){
                visited[i]=true;
                dfs(i, map);
            }    
        }
    }

Solution 3: BFS

    Map<Integer, Set<Integer>> map = new HashMap<>();
    boolean[] visited;

    public boolean validTree(int n, int[][] edges) {
        if (edges.length != n - 1)
            return false;
        visited = new boolean[n];
        for (int[] edge : edges) {
            map.putIfAbsent(edge[0], new HashSet<>());
            map.putIfAbsent(edge[1], new HashSet<>());
            map.get(edge[0]).add(edge[1]);
            map.get(edge[1]).add(edge[0]);
        }
        Queue<Integer> queue = new LinkedList<>();
        queue.offer(0);
        visited[0] = true;
        while (!queue.isEmpty()) {
            int size = queue.size();
            for (int i = 0; i < size; i++) {
                int node = queue.poll();
                if (map.containsKey(node)) {
                    Set<Integer> children = map.get(node);
                    for (int child : children) {
                        if (!visited[child]) {
                            queue.offer(child);
                            visited[child] = true;
                        }
                    }
                }
            }
        }
        for (int i = 0; i < n; i++) {
            if (!visited[i])
                return false;
        }
        return true;
    }

 

posted on 2022-02-04 11:55  阳光明媚的菲越  阅读(24)  评论(0编辑  收藏  举报