Java Solution using DFS
Java Solution Using DFS :\n\nSteps :\n\n1. Create adjList as a directed graph from input matrix .\n2. Do DFS for each node from 0 --> n\n3. Here we are consider
Java Solution Using DFS :\n\nSteps :\n\n1. Create adjList as a directed graph from input matrix .\n2. Do DFS for each node from 0 --> n\n3. Here we are considering each node as root/Parent and using DFS we will add this root/Parent into resultList of each nodes where we can reach from this root ( its children and their children ).\n4. We will use visited[] to identify if any of the child or their children are already visited for that particular root/parent, so to avoid duplicity and call reduction .\n\n\n\n```\n\nclass Solution {\n    \n    List<List<Integer>> adjList ;\n    List<List<Integer>> result ;\n    \n    public List<List<Integer>> getAncestors(int n, int[][] edges) {\n        \n        this.adjList = new ArrayList<>() ;\n        this.result = new ArrayList<>() ;\n\n        for(int i=0 ; i<n ; i++){\n            adjList.add(new ArrayList<>()) ;\n            result.add(new ArrayList<>()) ;\n        }\n        \n        for(int i=0 ; i<edges.length ; i++){\n            int start = edges[i][0] ;\n            int end = edges[i][1] ;\n            adjList.get(start).add(end) ;\n        }\n        \n        //  DFS for each node from 0 --> n , and add that node as root/parent into each reachable node and their child\n        //  Use visited[] to identify if any of the child or their childs are already visited for that perticular root/parent, \n        //  so will not add the root to avoid duplicacy and call reduction .\n        \n        for(int i=0 ; i<n ; i++){            \n            boolean visited[] = new boolean[n] ;\n            List<Integer> childList = adjList.get(i);\n            for(Integer child : childList){\n                if(!visited[child]){\n                    dfs( i , child , visited);\n                }                \n            }            \n        }\n        \n        return result ;\n    }\n    \n    public void dfs(int root , int node , boolean[] visited){\n        if(visited[node]){\n            return ;\n        }\n        visited[node] = true ;\n        result.get(node).add(root) ;\n        List<Integer> childList = adjList.get(node) ;\n        for(Integer child : childList){\n            if(!visited[child]){\n                dfs( root , child , visited);\n            }                \n        }\n    }\n}\n\n```
[]