C++(15ms) || BFS || Easy to Understand
\tvector distanceK(TreeNode root, TreeNode target, int k) {\n       \n        //making parents\n        unordered_mapmp;\n        queueq;\n        q.push(root);
\tvector<int> distanceK(TreeNode* root, TreeNode* target, int k) {\n       \n        //making parents\n        unordered_map<TreeNode*,TreeNode*>mp;\n        queue<TreeNode*>q;\n        q.push(root);\n        while(!q.empty())\n        {\n            TreeNode* parent=q.front();\n            q.pop();\n            if(parent->left){\n                mp[parent->left]=parent;\n                q.push(parent->left);\n            }\n            if(parent->right){\n                mp[parent->right]=parent;\n                q.push(parent->right);\n            }\n        } \n        \n        //traverse upto distance k\n        unordered_map<TreeNode*,bool>vis;\n        q.push(target);\n        vis[target]=1;\n        int count=0;\n        while(!q.empty())\n        {\n            int size=q.size();\n            if(count++==k) break;\n            for(int i=0; i<size; i++)\n            {\n                TreeNode* curr=q.front();\n                q.pop();\n                if(curr->left && !vis[curr->left]){\n                    q.push(curr->left);\n                    vis[curr->left]=1;\n                }\n                if(curr->right && !vis[curr->right]){\n                    q.push(curr->right);\n                    vis[curr->right]=1;\n                }\n                if(mp[curr] && !vis[mp[curr]]){\n                    q.push(mp[curr]);\n                    vis[mp[curr]]=1;\n                }\n            }\n        }\n        \n        //traverse queue to store in ans\n        vector<int>ans;\n        while(!q.empty()){\n            ans.push_back(q.front()->val);\n            q.pop();\n        }\n        return ans;\n    }\n\t\n**Like it? Please Upvote!!!**
['Breadth-First Search', 'C', 'C++']