<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) {}
 * };
 */
 #pragma GCC optimize("O3,unroll-loops")
class Solution {
public:

    vector<bool> path;

    void findpath(TreeNode* node,TreeNode* Target, vector<bool> &p)
    {
        if(node == nullptr) return;

        if(node == Target)
        {
            path = p;
            return;
        }
        p.push_back(0);
        findpath(node->left,Target,p);
        p.pop_back();
        p.push_back(1);
        findpath(node->right,Target,p);
        p.pop_back();
    }
    vector<int> ans;
    void findnodes(TreeNode* node, int d)
    {
        vector<TreeNode*> cur;
        cur.push_back(node);
        int cnt = 0;
        while(!cur.empty())
        {
            vector<TreeNode*> nxt;
            for(auto x: cur)
            {
                if(cnt ==d)
                    ans.push_back(x->val);
                if(cnt > d)
                    return;

                if(x->left!=nullptr)
                    nxt.push_back(x->left);
                if(x->right!=nullptr)
                    nxt.push_back(x->right);

                
            }
            cnt++;
            cur = nxt;
        } 
    }

    vector<int> distanceK(TreeNode* root, TreeNode* target, int k) {
        vector<bool> p;
        path.clear();
        findpath(root,target,p);
        TreeNode* cur;
        cur = root;
        for(int i =0;i<path.size();i++)
        {
            int dist = k +  i - path.size()-1;
            cout<<cur->val<<" "<<dist<<endl;
            if(dist == -1)
                ans.push_back(cur->val);
            if(path[i] ==0)
            {
                if(cur->right != nullptr)
                    findnodes(cur->right,dist);
            }
            if(path[i] ==1)
            {
                if(cur->left != nullptr)
                    findnodes(cur->left,dist);
            }

            if(path[i]==0)
                cur = cur->left;
            else
                cur = cur->right;
        }
        cout<<cur->val<<endl;
        findnodes(cur,k);
        return ans;
    }
};
14000