Easy to understand Python 🐍 graph solution
\nclass Solution:\n    def distanceK(self, root: TreeNode, target: TreeNode, k: int) -> List[int]:\n    \n        graph=defaultdict(list)\n        #create undir
```\nclass Solution:\n    def distanceK(self, root: TreeNode, target: TreeNode, k: int) -> List[int]:\n    \n        graph=defaultdict(list)\n        #create undirected graph\n        stack=[root]\n        while stack:\n            node=stack.pop()\n            if node==target:\n                targetVal=node.val\n            if node.left:\n                graph[node.val].append(node.left.val)\n                graph[node.left.val].append(node.val)\n                stack.append(node.left)\n            if node.right:\n                graph[node.val].append(node.right.val)\n                graph[node.right.val].append(node.val)\n                stack.append(node.right)\n        \n        #start BFS\n        q=deque([(targetVal,0)]) #startNode distance=0\n        seen=set()\n        seen.add(targetVal)\n        res=[]\n        while q:\n            node,depth=q.popleft()\n            if depth==k: \n                res.append(node)\n            if depth>k: break #no need to continue\n\n            for neigh in graph[node]:\n                if neigh not in seen:\n                    q.append((neigh,depth+1))\n                    seen.add(neigh)\n        return res\n```
['Python', 'Python3']