<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) {
        str.resize(0);
        sub_trees.clear();
        
        //stringize_df(root, str, 0);
        vector<TreeNode*> res;
        /*for(auto st:sub_trees) {
            if(st.second != nullptr)
                res.push_back(st.second);
        }*/
        traverse(root, res);
        return res;
    }
    
    int traverse(TreeNode* tree, vector<TreeNode*>& res) {    
        if(tree==nullptr) return 0;
        string descr = std::to_string(traverse(tree->left, res)) +','+
            std::to_string(tree->val)+','+
            std::to_string(traverse(tree->right, res));
        auto st = sub_trees.find(descr);
        int id;
        if(st==sub_trees.end()) {
            id = sub_trees.size()+1;
            sub_trees[descr] = id; // create new id
        } else {
            if(st->second>0) { // 2nd time
                id = st->second;
                st->second *=-1;
                // spit out results
                res.push_back(tree);
            } else 
                id = -st->second;
        }
        return id;
    }
    /*
    void stringize_df(TreeNode* tree, string& str, int node_start) {
        if(tree==nullptr) {
            str = str+"[]";
            return;
        }
        // leaf node
        if(tree->left == nullptr && tree->right == nullptr)
            str += "["+std::to_string(tree->val)+"]";
        else { // inner node
            // open bracket
            str += "[";
            stringize_df(tree->left, str, node_start+1);
            stringize_df(tree->right, str, str.size()); // after left node stringized
            // value and close bracket
            str += std::to_string(tree->val);
            str += "]";
        }
        // check node
        string sub_tree = str.substr(node_start);
        std::cout << sub_tree << std::endl;
        auto st = sub_trees.find(sub_tree);
        if(st==sub_trees.end())
            sub_trees[sub_tree] = nullptr; // seen for the first time
        else
            st->second = tree;
    }
    */
protected:
    string  str;
    unordered_map<string, int> sub_trees;
};
24750