public boolean validTree(int n, int[][] edges) {
        if (n <= 1) {
            return true;
        }

        Map<Integer, List<Integer>> graph = new HashMap<>();

        for (int[] edge : edges) {
            List<Integer> neighbors = graph.get(edge[0]);
            if (neighbors == null) {
                neighbors = new ArrayList<>();
                graph.put(edge[0], neighbors);
            }
            neighbors.add(edge[1]);
            neighbors = graph.get(edge[1]);
            if (neighbors == null) {
                neighbors = new ArrayList<>();
                graph.put(edge[1], neighbors);
            }
            neighbors.add(edge[0]);
        }

        boolean[] visited = new boolean[n];
        boolean hasCycle = isCycle(0, graph, -1, visited);
        if (hasCycle) {
            return false;
        }
        for (int i = 0; i < visited.length; i++) {
            if (!visited[i]) {
                return false;
            }
        }
        return true;
    }
Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes),
write a function to check whether these edges make up a valid tree.
https://leetcode.com/problems/graph-valid-tree/
src/com/interview/graph/ValidTree.java