Simple Python solution. Create Graph and dfs
\n# Code\n\nclass Solution:\n    def distanceK(self, root: TreeNode, target: TreeNode, k: int) -> List[int]:\n        adj = defaultdict(list)\n        vis = set
\n# Code\n```\nclass Solution:\n    def distanceK(self, root: TreeNode, target: TreeNode, k: int) -> List[int]:\n        adj = defaultdict(list)\n        vis = set()\n        res = []\n\n        def makeGraph(node):\n            if node.left:\n                adj[node].append(node.left)\n                adj[node.left].append(node)\n                makeGraph(node.left)\n            if node.right:\n                adj[node].append(node.right)\n                adj[node.right].append(node)\n                makeGraph(node.right)\n        \n        makeGraph(root)\n        \n        def dfs(node, d):\n            if d < k:\n                vis.add(node)\n                d += 1\n                for u in adj[node]:\n                    if u not in vis:\n                        dfs(u, d)\n            else:\n                res.append(node.val)\n        \n        dfs(target, 0)\n        return res\n```
['Python3']