<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>

class Solution {
public:

    void traverse_tree(TreeNode* root, TreeNode* parent_node,
                        vector<TreeNode*>& parent,
                        vector<vector<TreeNode*>>& child) {
        if (root == NULL)
            return;

        parent[root->val] = parent_node;

        if (root->left != NULL) {
            child[root->val][0] = root->left;
            traverse_tree(root->left, root, parent, child);
        }

        if (root->right != NULL) {
            child[root->val][1] = root->right;
            traverse_tree(root->right, root, parent, child);
        }
    }

    vector<int> distanceK(TreeNode* root, TreeNode* target, int k) {
        vector<int> ret;
        
        int visited[505] = {0, };
        vector<TreeNode*> parent(505, NULL);
        vector<vector<TreeNode*>> child(505, vector<TreeNode*>(2, NULL));

        traverse_tree(root, NULL, parent, child);

        queue<pair<TreeNode*, int>> q1;
        q1.emplace(target, 0);
        visited[target->val] = 1;

        while (!q1.empty()) {
            TreeNode* temp = q1.front().first;
            int cur_depth = q1.front().second;
            q1.pop();

            if (cur_depth == k) {
                ret.push_back(temp->val);
                continue;
            }

            // parent
            if (parent[temp->val] != NULL) {
                if (!visited[parent[temp->val]->val]) {
                    visited[parent[temp->val]->val] = 1;
                    q1.emplace(parent[temp->val], cur_depth + 1);
                }
            }

            // left
            if (child[temp->val][0] != NULL) {
                if (!visited[child[temp->val][0]->val]) {
                    visited[child[temp->val][0]->val] = 1;
                    q1.emplace(child[temp->val][0], cur_depth + 1);
                }
            }

            // right
            if (child[temp->val][1] != NULL) {
                if (!visited[child[temp->val][1]->val]) {
                    visited[child[temp->val][1]->val] = 1;
                    q1.emplace(child[temp->val][1], cur_depth + 1);
                }
            }
        }

        return ret;
    }
};
16500