<p>Given the <code>root</code>&nbsp;of a binary tree, return all <strong>duplicate subtrees</strong>.</p>

<p>For each kind of duplicate subtrees, you only need to return the root node of any <b>one</b> of them.</p>

<p>Two trees are <strong>duplicate</strong> if they have the <strong>same structure</strong> with the <strong>same node values</strong>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/08/16/e1.jpg" style="width: 450px; height: 354px;" />
<pre>
<strong>Input:</strong> root = [1,2,3,4,null,2,4,null,null,4]
<strong>Output:</strong> [[2,4],[4]]
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/08/16/e2.jpg" style="width: 321px; height: 201px;" />
<pre>
<strong>Input:</strong> root = [2,1,1]
<strong>Output:</strong> [[1]]
</pre>

<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/08/16/e33.jpg" style="width: 450px; height: 303px;" />
<pre>
<strong>Input:</strong> root = [2,2,2,3,null,3,null]
<strong>Output:</strong> [[2,3],[3]]
</pre>

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

<ul>
	<li>The number of the nodes in the tree will be in the range <code>[1, 5000]</code></li>
	<li><code>-200 &lt;= Node.val &lt;= 200</code></li>
</ul>

/**
 * 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:

    vector<TreeNode*> findDuplicateSubtrees(TreeNode* root) {
        vector<TreeNode*> v;
        queue<TreeNode*> q;
        q.push(root);
        while(!q.empty()){
            TreeNode* n = q.front();
            q.pop();
            v.push_back(n);
            if(n->left) q.push(n->left);
            if(n->right) q.push(n->right);
        }
        unordered_map<TreeNode*, short> nodeID;
        unordered_map<long, short> structureID;
        unordered_map<short, short> seen;
        short nextID = 410;
        vector<TreeNode*> ret;
        for(int i = v.size() - 1; i >= 0; i--){
            TreeNode* n = v[i];
            if(!(n->left || n->right)){
                nodeID[n] = n->val + 200;
                if(seen[n->val + 200]++ == 1) ret.push_back(n);
                continue;
            }
            long nID = ((long)(n->val + 200)) << 32;
            if(n->left) nID += nodeID[n->left] << 16;
            if(n->right) nID += nodeID[n->right];
            if(structureID.count(nID)){
                // we have seen this subtree before. give this node that subtree's ID,
                // and if this is precisely the 2nd time we've seen it, add n to ret.
                nodeID[n] = structureID[nID];
                // cout << "found ID " << structureID[nID] << "!" << endl;
                if(seen[structureID[nID]]++ == 1) ret.push_back(n);
            } else{
                // assign this subtree a new ID, assign this node that ID,
                // and note that we've seen it once
                structureID[nID] = nextID;
                seen[nextID] = 1;
                nodeID[n] = nextID;
                // cout << "assigning id " << nextID << endl;
                nextID++;
            }
        }
        return ret;
    }
};
24750