<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")
    #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
    static const bool __boost = []() {
	cin.tie(nullptr);
	cout.tie(nullptr);
	return std::ios_base::sync_with_stdio(false);
	}();


const size_t BUFFER_SIZE = 0x6fafffff;
alignas(std::max_align_t) char buffer[BUFFER_SIZE];
size_t buffer_pos = 0;
void* operator new(size_t size) {
	constexpr std::size_t alignment = alignof(std::max_align_t); // Default maximum alignment
	size_t padding = (alignment - (buffer_pos % alignment)) % alignment;
	size_t total_size = size + padding;
	char* aligned_ptr = &buffer[buffer_pos + padding];
	buffer_pos += total_size;
	return aligned_ptr;
}
void operator delete(void* ptr) {
}

// Overload the delete[] operator to do nothing
void operator delete[](void* ptr) {}

class Solution {
public:
    
    vector<int> nodes(TreeNode* root, int k) {
        if (k < 0) {
            return {};
        }
        if (k == 0) {
            return {root->val};
        }
        vector<int> ans;
        if (k == 1) {
            if (root->left) {
                ans.push_back(root->left->val);
            }
            if (root->right) {
                ans.push_back(root->right->val);
            }
            return ans;
        }
        if (root->left) {
            for (int i : nodes(root->left, k-1)) {
                ans.push_back(i);
            }
        }
        if (root->right) {
            for (int i : nodes(root->right, k-1)) {
                ans.push_back(i);
            }
        }
        return ans;
    }

    vector<int> distanceK(TreeNode* root, TreeNode* target, int k) {
        stack<pair<vector<char>, TreeNode*>> dfs;
        dfs.push({{}, root});
        vector<char> path;
        while (!dfs.empty()) {
            vector<char> temp = dfs.top().first;
            TreeNode* node = dfs.top().second;
            dfs.pop();
            if (node == target) {
                path = temp;
                break;
            }
            if (node->left != nullptr) {
                temp.push_back(0);
                dfs.push({temp, node->left});
                temp.pop_back();
            }
            if (node->right != nullptr) {
                temp.push_back(1);
                dfs.push({temp, node->right});
                temp.pop_back();
            }
        }
        vector<int> ans;
        TreeNode* node = root;
        int i = 0;
        while (node != target) {
            if (k+i-path.size() == 0) {
                ans.push_back(node->val);
            }
            if (path[i] == 0) {
                if (node->right){
                    for (int j : nodes(node->right, k+i-path.size()-1)) {
                        ans.push_back(j);
                    }
                }
                node = node -> left;
            }
            if (path[i] == 1) {
                if (node->left) {
                    for (int j : nodes(node->left, k+i-path.size()-1)) {
                        ans.push_back(j);
                    }
                }
                node = node -> right;
            }
            i++;
        }
        for (int j : nodes(target, k)) {
            ans.push_back(j);
        }
        return ans;
    }
};
10700