<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:
    typedef long long ll;
    const ll p = 61;
    const ll md = 1e9+7;
    ll op = '(', cl = ')';

    ll bin(ll a, ll b) {
        ll res = 1;
        while (b) {
            if (b & 1)
                res = res * a % md;
            a = a * a % md;
            b >>= 1;
        }
        return res;
    }

    ll hash(int val, ll lft, int rgt) {
        val += 205;
       
       // hashin --->  val+ "("+lft+")"+"("+rgt")"
        ll hs = val + op * p % md;
        int i = 1;
        for (i = 2; lft; i++) {
            hs = (hs + lft % 10 * bin(p, i) % md) % md;
            lft /= 10;
        }

        hs = (hs + cl * bin(p, i) % md + op * bin(p, i + 1) % md) % md;
        for (i += 2; rgt; i++) {
            hs = (hs + rgt % 10 * bin(p, i) % md) % md;
            rgt /= 10;
        }
        hs = (hs + cl * bin(p, i)) % md;

             return hs;
    }

    unordered_map<ll, int> mp;
    vector<TreeNode*> ans;

    ll rec(TreeNode* root) {
        if (root == NULL)
            return '$';

        ll left = rec(root->left);
        ll rgt = rec(root->right);

        ll hs = hash(root->val, left, rgt);
        mp[hs]++;
        if (mp[hs] == 2) {
            ans.push_back(root);
        }
        return hs;
    }

    vector<TreeNode*> findDuplicateSubtrees(TreeNode* root) {
        ans.clear();
        mp.clear();
        rec(root);
        return ans;
    }
};
23821