<p>Given the <code>root</code> of a binary tree, the value of a target node <code>target</code>, and an integer <code>k</code>, return <em>an array of the values of all nodes that have a distance </em><code>k</code><em> from the target node.</em></p>

<p>You can return the answer in <strong>any order</strong>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://s3-lc-upload.s3.amazonaws.com/uploads/2018/06/28/sketch0.png" style="width: 500px; height: 429px;" />
<pre>
<strong>Input:</strong> root = [3,5,1,6,2,0,8,null,null,7,4], target = 5, k = 2
<strong>Output:</strong> [7,4,1]
Explanation: The nodes that are a distance 2 from the target node (with value 5) have values 7, 4, and 1.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> root = [1], target = 1, k = 3
<strong>Output:</strong> []
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li>The number of nodes in the tree is in the range <code>[1, 500]</code>.</li>
	<li><code>0 &lt;= Node.val &lt;= 500</code></li>
	<li>All the values <code>Node.val</code> are <strong>unique</strong>.</li>
	<li><code>target</code> is the value of one of the nodes in the tree.</li>
	<li><code>0 &lt;= k &lt;= 1000</code></li>
</ul>

/**
 * 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:
vector<int>h1(TreeNode* root, TreeNode* target, int k,map<TreeNode*,TreeNode*>mp){
    queue<TreeNode*>q;
    unordered_set<TreeNode*>vis;
    vis.insert(target);
    q.push(target);
    int c=0;vector<int>res;
    while(!q.empty()){
        int size=q.size();
        if(c==k){
            while(!q.empty()){
                auto it=q.front();q.pop();res.push_back(it->val);
            }
            return res;
        }
        while(size--){
            auto f=q.front();q.pop();
            if(f->right && vis.find(f->right)==vis.end()){vis.insert(f->right);q.push(f->right);}
            if(f->left && vis.find(f->left)==vis.end()){vis.insert(f->left);q.push(f->left);}
            if(mp.find(f)!=mp.end() && vis.find(mp[f])==vis.end()){vis.insert(mp[f]);q.push(mp[f]);}
        }
        c++;
    }return res;
    
}
vector<int> helper(TreeNode* root, TreeNode* target, int k){

    map<TreeNode*,TreeNode*>mp;stack<TreeNode*>st;
    st.push(root);TreeNode* n=nullptr;
    while(!st.empty()){
        auto it=st.top();
        st.pop();
        if(it==target){n=it;}
        if(it->right){mp[it->right]=it;st.push(it->right);}
        
        if(it->left){mp[it->left]=it;st.push(it->left);}
        
    }vector<int>ans;
    ans=h1(root,target,k,mp);return ans;

}

    vector<int> distanceK(TreeNode* root, TreeNode* target, int k) {
        return helper(root,target,k);
    }
};
15200