<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 {
private:
    bool match(TreeNode* root1, TreeNode* root2) {
        if (root1 == nullptr && root2 == nullptr) {
            return true;
        }
        if (root1 == nullptr || root2 == nullptr) {
            return false;
        }
        return root1->val == root2->val && match(root1->left, root2->left) && match(root1->right, root2->right);
    }

    int serialize(TreeNode* root, std::vector<std::pair<TreeNode*, int>>* output) {
        if (root == nullptr) {
            return 0;
        }
        int count = 0;
        count += serialize(root->left, output);
        count += serialize(root->right, output);
        output->push_back({root, count + 1});
        return count + 1;
    }
public:
    vector<TreeNode*> findDuplicateSubtrees(TreeNode* root) {
        std::vector<std::pair<TreeNode*, int>> nodes;
        serialize(root, &nodes);
        std::vector<TreeNode*> ans;
        std::unordered_set<int> visited;
        for (int i=0; i<nodes.size(); ++i) {
            if (visited.find(i) != visited.end()) {
                continue;
            }
            bool found = false;
            for (int j=i+1; j<nodes.size(); ++j) {
                if (nodes[i].second == nodes[j].second && match(nodes[i].first, nodes[j].first)) {
                    visited.insert(j);
                    found = true;
                }
            }
            if (found) {
                ans.push_back(nodes[i].first);
            }
        }
        return ans;
    }
};
22893