[Java] Simple DFS w/ brief explanation, Beat 100%
\npublic List<List<Integer>> getAncestors(int n, int[][] edges) {\n\t\t// Build directed graph\n        List<Integer>[] graph = new ArrayList[n];\n        for (
```\npublic List<List<Integer>> getAncestors(int n, int[][] edges) {\n\t\t// Build directed graph\n        List<Integer>[] graph = new ArrayList[n];\n        for (int[] e : edges) {\n            if (graph[e[0]] == null) \n                graph[e[0]] = new ArrayList<>();\n            graph[e[0]].add(e[1]);\n        }\n        \n        List<List<Integer>> res = new ArrayList<>();\n        for (int i = 0; i < n; i++) \n            res.add(new ArrayList<>());\n        \n\t\t// Since the list of ancestors should be sorted in ascending order, we iterator from node 0 to node n - 1\n\t\t// Use DFS to find the nodes that node i can reach\n        for (int i = 0; i < n; i++) {\n            if (graph[i] == null) continue;\n\t\t\t// We need to check whether we visited the node before\n\t\t\t// Example: 0 -> 3 -> 6, 0 -> 4 -> 6, if we DFS from node 0, we may visite node 6 twice\n            boolean[] visited = new boolean[n];\n\t\t\t// We do one step of DFS here since node i is not the ancestor of itself.\n\t\t\t// Then in the following steps of DFS, we add node i to the node next\'s ancestor\n            for (int next : graph[i]) {\n                if (visited[next]) continue;\n                visited[next] = true;\n                dfs(i, next, graph, res, visited);\n            }\n        }\n        return res;\n    }\n    private void dfs(int ancestor, int node, List<Integer>[] graph, List<List<Integer>> res, boolean[] visited) {\n        res.get(node).add(ancestor);\n        if (graph[node] == null) return;\n        for (int next : graph[node]) {\n            if (visited[next]) continue;\n            visited[next] = true;\n            dfs(ancestor, next, graph, res, visited);\n        }\n    }\n```
[]