2 bfs
Intuition\n Describe your first thoughts on how to solve this problem. \nFind distance of the target node to other nodes .\nwe will have to store neighbours of 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nFind distance of the target node to other nodes .\nwe will have to store neighbours of each node. \n\n\n\n# Approach\n<!-- Describe your approach to solving the problem. --> \n# 1.to find the neighbours\n    1. we make an undirected path from root to its left child and root\n    to  right child\n    2. we add root\'s left and root\'s right to its adjacency list \n    3. we add root to the adjacency list of its children \n    4. 2 and 3 are done only if the children is not null(None)\n    \n# 2.find node at distance k \n    1. start with target node in the queue\n\n    2. iterate until k>0(we get the nodes at distance k) \n\n    3. add an extra condition while queue is not empty. this is \n        beacause if the  distance of all nodes from the target is  \n        less than   k .\n    4. affter the bfs(loop) ends, add the value all the nodes in the\n      ans list to be returned.\n    5. **the bfs could also be done by having node and dist in the \n        queue , the nodes with distance k would be appended to the\n        ans list\n\n\n\n# Complexity\n- Time complexity:O(n*log(n))\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(n)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\n# Definition for a binary tree node.\n# class TreeNode:\n#     def __init__(self, x):\n#         self.val = x\n#         self.left = None\n#         self.right = None\n\nclass Solution:\n    def distanceK(self, root: TreeNode, target: TreeNode, k: int) -> List[int]:\n        q=[root]\n        d=defaultdict(list)#store neighbours\n        while(q):\n            \n            for i in range(len(q)):\n                x=q.pop(0)\n                if x.left:\n                    d[x].append(x.left)\n                    d[x.left].append(x)\n                    q.append(x.left)\n                if x.right:\n                    d[x.right].append(x)\n                    d[x].append(x.right)\n                    q.append(x.right)\n\n\n        #find the nodes at distance k\n        q=[target]\n        vis=set()\n        vis.add(target)\n        while(q and k>0):\n            \n            for i in range(len(q)):\n                x=q.pop(0)\n                for node in d[x]:\n                    if node not in vis:\n                        vis.add(node)\n                        q.append(node)\n            k-=1 \n        ans=[]\n        \n        for node in q:\n            ans.append(node.val)\n        return ans\n\n```
['Tree', 'Breadth-First Search', 'Binary Tree', 'Python', 'Python3']