Easy to Understand || Simple DFS || 100% accepted
Intuition\nPretty easy to understand! 100% accepted with all testcases.Simple DFS Traversal.\n\n# Approach\nUsing Depth First Search Traversal is enough to solv
# Intuition\nPretty easy to understand! 100% accepted with all testcases.Simple DFS Traversal.\n\n# Approach\nUsing Depth First Search Traversal is enough to solve!!!\nAnd use of default dictionary reduces Time complexity!\n\n\n# Complexity\n- Time complexity:\n O(n\xD7(n+E))\n\n- Space complexity:\n O(n**2+E)\nwhere \'n\' is no.of Veritces\n      \'E\' is no.of Edges\n\nIf you like it Please upvote...\n# Code\n```\nclass Solution:\n    def getAncestors(self, n: int, edges: List[List[int]]) -> List[List[int]]:\n        overall_result=[]\n        d=defaultdict(list)\n\n        #Here you will get immediate ancestors \n        for u,v in edges:\n            d[v].append(u)\n         \n        def dfs(v,visited):\n            #Traversing on immediate ancestors is enough!\n            for i in d[v]:\n                if(i not in visited):\n                    visited.add(i)\n                    dfs(i,visited)\n\n        for i in range(n):\n            visited=set()\n            dfs(i,visited)\n            overall_result.append(sorted(visited))\n        return overall_result\n```
['Python3']