<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 {
    using Table = unordered_map<int, unordered_map<size_t, pair<TreeNode*, bool>>>;

    size_t hashFunction(TreeNode* root, size_t left, size_t right) {
        size_t me = hash<int>{}(root->val);
        string tmp = to_string(me) + "L" + to_string(left) + "R" + to_string(right);
        return hash<string>{}(tmp);
    }

    size_t traverse(TreeNode* root, vector<TreeNode*>& report, Table& record) {
        size_t ret;
        size_t left = 201;
        size_t right = 202;

        if (root->left) {
            left = traverse(root->left, report, record);
        }

        if (root->right) {
            right = traverse(root->right, report, record);
        }

        ret = hashFunction(root, left, right);

        auto& temp = record[root->val];

        auto itr = temp.find(ret);

        if (itr != temp.end()) {
            if (!itr->second.second) {
                itr->second.second = true;
                report.push_back(itr->second.first);
            }
        } else {
            temp[ret] = {root, false};
        }

        return ret;
    }

public:
    vector<TreeNode*> findDuplicateSubtrees(TreeNode* root) {
        vector<TreeNode*> ret;
        Table record;

        traverse(root, ret, record);

        return ret;
    }
};
28465