Simple solution using array indexing method, Step wise  image Illustration , C++
Intuition\n Describe your first thoughts on how to solve this problem. \n## Tree is a Data structure in which we only have addrees  to its child nodes . So how 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n## Tree is a Data structure in which we only have addrees  to its child nodes . So how can we calculate Distance between any two nodes(let say X and Y are our Node).\n-->the answer is simple , if we somehow get a common parent(or node which exist on upper level , let say A) node of both the nodes then we can simply find the distance between both the nodes by distance(X->Y) = distance(A->X)+distance(A->Y)\n![Screenshot 2023-07-11 114110.png](https://assets.leetcode.com/users/images/a07b6d13-9c56-475f-9810-804b4d6f5062_1689055892.4989724.png)\n\n## can we store a treeNode values into array in a Systematic Manner?\n--> yes we can . let say  we have a nodeVal stored in array at index i then its left children will be present at index (i\\*2)+1 th index and right children will be at (i\\*2)+2 th index;\n \n![Screenshot 2023-07-11 114735.png](https://assets.leetcode.com/users/images/0b8077e8-8b55-47f4-861d-b8a071c506d6_1689056298.266868.png)\n\n<!-- Describe your approach to solving the problem. -->\n## Now How will we get the common parent of our nodes including them?\n--> distance(X->Y) = distance(A->X)+distance(A->Y)\n    also the distance(A->X) = distance(X->A)\n-->  so can we write\n    distance(X->Y) = distance(X->A)+distance(Y->A)\n#### now how to calculate distance (X->A)\nif index(X) is odd reduce it by 1 otherwise reduce it by 2 , and then divide it by 2 .\nperform it until we did not reach A\nhere each set of opration indicates 1 unit distance\nDistance(X->A) = 2 { [(4-2)/2] then [(1-1)/2] }\n![Screenshot 2023-07-11 115345.png](https://assets.leetcode.com/users/images/bd6159e9-f154-45a5-8d8d-00ee6619006e_1689056671.7199218.png)\n\nDistance(Y->A) = 1 { [(2-2)/2] }\n![Screenshot 2023-07-11 115640.png](https://assets.leetcode.com/users/images/7391eacc-5275-4031-b0a8-18248ab273ce_1689056822.847989.png)\n\nso total distance = 2+1 = 3\n\n# Approach\n\nfirst find the index of target in virtual array (we dont have to create a actual array) by findPosition function\n\nthen again traverse the whole array and find if the cuuernt node is at distace of k unit to target node\n\n\n# Complexity\n### Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nO(n) to find position of target node\nclaulation of distance will take max(log(height Y) , log(height Y))\nfor each node \nso total will be\n##### O(n + nlog(n)) = O(nlog(n))\n\n### Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n  ##### O(1)\n\n## Drawback/flows : integer "cureent" can overflow in case of skewed tree\n\n# Hit Upvote if you like my Solution\n\n# Code\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    // function to get Index of target\n\n    void findPosition(TreeNode* root , TreeNode* target,int curr ,  int& pos){\n        if(root == nullptr){\n            return;\n        }\n        else if(root == target){\n            pos = curr;\n            return;\n        }\n        else{\n            findPosition(root->left , target , curr*2+1 , pos);\n            findPosition(root->right , target , curr*2+2 , pos);\n        }\n    }\n\n    // function to get all required node values\n\n    void get(TreeNode* root , int curr , int pos , int k ,vector<int> &ans){\n        if(root == nullptr){\n            return;\n        }\n        else{\n            int x = curr , y = pos , distance = 0;\n            //loop to calculate distance\n            while( x != y ){\n                if(x>y){\n                   x%2 == 0 ? x-=2 : x-=1 ;\n                   x/=2;\n                }\n                else{\n                   y%2 == 0 ? y-=2 : y-=1 ;\n                   y/=2;\n                }\n                distance++;\n            }\n            if(distance == k){\n                ans.push_back(root->val);\n            }\n            get(root->left,curr*2+1 , pos , k , ans );\n            get(root->right,curr*2+2 , pos , k , ans );\n        }\n    }\n    \n    vector<int> distanceK(TreeNode* root, TreeNode* target, int k) {\n        int pos = 0;\n        findPosition(root , target , 0 , pos );\n        vector<int> ans;\n        get(root , 0 , pos , k , ans );\n        return ans;\n    }\n};\n```
['Array', 'Tree', 'Binary Tree', 'C++']