C++ || DFS
Connect with me on LinkedIn: https://www.linkedin.com/in/abhay5349singh/\n\n\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n *     int val;\
**Connect with me on LinkedIn**: https://www.linkedin.com/in/abhay5349singh/\n\n```\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n *     int val;\n *     TreeNode *left;\n *     TreeNode *right;\n *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}\n * };\n */\nclass Solution {\npublic:\n    \n\t// building path from Root of Tree to Target Node\n    bool rootToNodePath(TreeNode* node, vector<TreeNode*> &path, TreeNode* target){\n        if(node==NULL) return false;\n        \n        path.push_back(node);\n        \n        if(node==target) return true;\n        \n        if(rootToNodePath(node->left, path, target) || rootToNodePath(node->right, path, target)) return true;\n        \n        path.pop_back();\n        return false;\n    }\n    \n\t// Storing all nodes at particular distance \'k\' from current node\n    void getNodes(TreeNode* root, int k, vector<int> &ans, TreeNode* blocker){\n        if(root==NULL || k<0 || root==blocker) return;\n        \n        if(k==0){\n            ans.push_back(root->val);\n            return;\n        }\n        getNodes(root->left,k-1,ans,blocker);\n        getNodes(root->right,k-1,ans,blocker);\n    }\n    \n    vector<int> distanceK(TreeNode* root, TreeNode* target, int k) {\n        vector<TreeNode*> path;\n        rootToNodePath(root, path, target);\n        \n        reverse(path.begin(),path.end());\n        \n        TreeNode* blocker=NULL; // latest node for which nodes at distance \'k\' are stored to prevent duplicate & incorrect entries\n         vector<int> ans;\n        for(int i=0;i<path.size();i++){\n            getNodes(path[i],k-i,ans,blocker);\n            blocker=path[i];\n        }\n        return ans;\n    }\n};\n```
['Depth-First Search', 'C++']