<p>Given the <code>root</code> of a binary tree, the depth of each node is <strong>the shortest distance to the root</strong>.</p>

<p>Return <em>the smallest subtree</em> such that it contains <strong>all the deepest nodes</strong> in the original tree.</p>

<p>A node is called <strong>the deepest</strong> if it has the largest depth possible among any node in the entire tree.</p>

<p>The <strong>subtree</strong> of a node is a tree consisting of that node, plus the set of all descendants of that node.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://s3-lc-upload.s3.amazonaws.com/uploads/2018/07/01/sketch1.png" style="width: 600px; height: 510px;" />
<pre>
<strong>Input:</strong> root = [3,5,1,6,2,0,8,null,null,7,4]
<strong>Output:</strong> [2,7,4]
<strong>Explanation:</strong> We return the node with value 2, colored in yellow in the diagram.
The nodes coloured in blue are the deepest nodes of the tree.
Notice that nodes 5, 3 and 2 contain the deepest nodes in the tree but node 2 is the smallest subtree among them, so we return it.
</pre>

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

<pre>
<strong>Input:</strong> root = [1]
<strong>Output:</strong> [1]
<strong>Explanation:</strong> The root is the deepest node in the tree.
</pre>

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

<pre>
<strong>Input:</strong> root = [0,1,3,null,2]
<strong>Output:</strong> [2]
<strong>Explanation:</strong> The deepest node in the tree is 2, the valid subtrees are the subtrees of nodes 2, 1 and 0 but the subtree of node 2 is the smallest.
</pre>

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

<ul>
	<li>The number of nodes in the tree will be in the range <code>[1, 500]</code>.</li>
	<li><code>0 &lt;= Node.val &lt;= 500</code></li>
	<li>The values of the nodes in the tree are <strong>unique</strong>.</li>
</ul>

<p>&nbsp;</p>
<p><strong>Note:</strong> This question is the same as 1123: <a href="https://leetcode.com/problems/lowest-common-ancestor-of-deepest-leaves/" target="_blank">https://leetcode.com/problems/lowest-common-ancestor-of-deepest-leaves/</a></p>

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    TreeNode* subtreeWithAllDeepest(TreeNode* root) {
        queue<pair<int, TreeNode*>> q;
        q.push({0, root});
        vector<int> used;
        vector<int> parent;
        if (used.size() <= root->val){
            int tmp = used.size();
            for (int i = 0; i < root->val - tmp + 1; i++) used.push_back(0), parent.push_back(-1);
        }
        unordered_map<int, TreeNode*> mp;
        used[root->val] = 1;
        int mx_d = 0;
        TreeNode* mx = root;
        mp[root->val] = root;
        vector<int> all_mx;
        while (!q.empty()){
            pair<int, TreeNode*> tmp = q.front();
            mp[tmp.second->val] = tmp.second;
            if (mx_d <= tmp.first){
                // if (tmp.second->left != nullptr && tmp.second->right != nullptr){
                //     mx_d = tmp.first;
                //     mx = tmp.second;
                // }else if ((tmp.second->left == nullptr && tmp.second->right != nullptr) or (tmp.second->left != nullptr && tmp.second->right == nullptr)){
                //     mx_d = tmp.first + 1;
                //     if (tmp.second->left != nullptr)
                //         mx = tmp.second->left;
                //     else
                //         mx = tmp.second->right;
                //     // cout << mx->val << '\n';
                // }
                if (mx_d == tmp.first){
                    all_mx.push_back(tmp.second->val);
                }else{
                    mx_d = tmp.first;
                    all_mx.clear();
                    all_mx.push_back(tmp.second->val);
                }
            }
            q.pop();
            if (tmp.second->left != nullptr){
                if (used.size() <= tmp.second->left->val){
                    int ttmp = used.size();
                    for (int i = 0; i < tmp.second->left->val - ttmp + 1; i++) used.push_back(0), parent.push_back(-1);
                }
                if (used[tmp.second->left->val] == 0){
                    used[tmp.second->left->val] = 1;
                    parent[tmp.second->left->val] = tmp.second->val;
                    q.push({tmp.first + 1, tmp.second->left});
                }
            }
            if (tmp.second->right != nullptr){
                if (used.size() <= tmp.second->right->val){
                    int ttmp = used.size();
                    for (int i = 0; i < tmp.second->right->val - ttmp + 1; i++) used.push_back(0), parent.push_back(-1);
                }
                if (used[tmp.second->right->val] == 0){
                    used[tmp.second->right->val] = 1;
                    parent[tmp.second->right->val] = tmp.second->val;
                    q.push({tmp.first + 1, tmp.second->right});
                }
            }
        }
        while (true){
            bool ok = true;
            for (int i = 1; i < all_mx.size(); i++){
                if (parent[all_mx[i]] != parent[all_mx[0]]){
                    ok = false;
                    break;
                }
            }
            if (!ok){
                for (int i = 0; i < all_mx.size(); i++){
                    all_mx[i] = parent[all_mx[i]];
                }
            }
            if (ok) break;
        }
        int answ = root->val;
        if (all_mx.size() > 1)
            answ = parent[all_mx[0]];
        else
            answ = all_mx[0];
        
        return mp[answ];
    }
};
16200