easy approach using recursion only
IntuitionIntuition (Soch)
Iss problem ka aim hai binary tree me ek target node ke distance k par sabhi nodes ko dhoondhna. Iske liye do kaam karne padenge:->Tar
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
Intuition (Soch)
Iss problem ka aim hai binary tree me ek target node ke distance k par sabhi nodes ko dhoondhna. Iske liye do kaam karne padenge:

->Target node ke subtree ko explore karna: Target ke neeche wale nodes ko distance 𝑘 par dhoondhna.
->Tree ke upar ki taraf traverse karna: Jo nodes target ke subtree ke bahar hain unko distance 𝑘 par dhoondhna.


Simple Approach:

->Agar hum target node tak pahuch jayein, toh wahan se downward nodes ko easily find kar sakte hain using recursive calls.
->Agar target node se upar ja rahe hain, toh opposite subtree me bhi 𝑘 distance ke nodes ko check karna hoga.

# Approach
<!-- Describe your approach to solving the problem. -->

Helper Function (fun):
Yeh function kisi node ke subtree me se 𝑘 distance ke nodes ko collect karega.

2)Recursive Solve Function (solve):

->Agar current node target ke barabar ho, toh uske subtree ke nodes ko collect karo.
->Agar target node left subtree me mile, toh current node ka distance calculate karke right subtree ko explore karo.
->Similarly, agar target node right subtree me mile, toh left subtree ko explore karo.
3)Main Function (distanceK):
Yeh solve function ko call karega aur answer vector ko return karega.

# Complexity
- Time complexity:
0(N)
- Space complexity:
0(H)

# Code
```cpp []
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
     void fun(TreeNode* root, int k, vector<int>& ans) {
        if (!root || k < 0) return;
        if (k == 0) {
            ans.push_back(root->val);
            return;
        }
        fun(root->left, k - 1, ans);
        fun(root->right, k - 1, ans);
    }

    int solve(TreeNode* root,TreeNode* target,int k,vector<int>&ans){
        if(!root)return -1;
        if(root==target){
            fun(root,k,ans);
            return 0;
        }
        int ld=solve(root->left,target,k,ans);
        if (ld != -1) {
            if (ld + 1 == k) {
                ans.push_back(root->val);
            } else {
                fun(root->right, k - ld - 2, ans);
            }
            return ld + 1;
        }
        int rd = solve(root->right, target, k, ans);
        if (rd != -1) {
            if (rd + 1 == k) {
                ans.push_back(root->val);
            } else {
                fun(root->left, k - rd - 2, ans);
            }
            return rd + 1;
        }
        return -1;
    }
    vector<int> distanceK(TreeNode* root, TreeNode* target, int k) {
        vector<int>ans;
        solve(root,target,k,ans);
        return ans;
    }
};
```
['C++']