<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.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def findDuplicateSubtrees(self, root: Optional[TreeNode]) -> List[Optional[TreeNode]]:
        ans = []

        
        stack = [root]
        h = {}
        nodeToCode = {}
        #code = self.generateCode(root,nodeToCode)
        #print(code)
        #return ans
        while(stack):
            cur = stack.pop()
            code,nodeToCode = self.generateCode(cur,nodeToCode)
            if(code in h):
                if(h[code]==1):
                    ans.append(cur)
                h[code]+=1
            else:
                h[code] =1
            if(cur.left):
                stack.append(cur.left)
            if(cur.right):
                stack.append(cur.right)
        return ans

    def generateCode(self, root,nodeToCode):
        if(root is None):
            return ""
        if(root in nodeToCode):
            return nodeToCode[root],nodeToCode

        codeLeft = ""
        if(root.left!=None):
            if(root.left in nodeToCode):
                codeLeft= nodeToCode[root.left]
                codeLeft +="<"
            else:
                codeLeft,nodeToCode= self.generateCode(root.left,nodeToCode)
                nodeToCode[root.left] = codeLeft
            codeLeft +="<"  

        codeRight = ""
        if(root.right!=None):
            if(root.right in nodeToCode):
                codeRight = nodeToCode[root.right]
                
            else:
                codeRight,nodeToCode = self.generateCode(root.right,nodeToCode)
                nodeToCode[root.right] = codeRight
            codeRight = ">"+codeRight
        ans = codeLeft+str(root.val)+codeRight
        nodeToCode[root] = ans
        return ans,nodeToCode

19113