<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]]:
        subtree_map = {}
        res = []

        def dfs(root):
            if not root:
                return "null"

            s = f"{root.val},{dfs(root.left)},{dfs(root.right)}"
            
            if s in subtree_map and subtree_map[s] == 1:
                res.append(root)
                subtree_map[s] += 1
            elif s not in subtree_map:
                subtree_map[s] = 1
            
            return s

        dfs(root)
        return res


    def findDuplicateSubtrees_not(self, root: Optional[TreeNode]) -> List[Optional[TreeNode]]:
        def is_duplicate(root1, root2):
            if not root1 and not root2:
                return True

            if not root1 or not root2 or root1.val != root2.val:
                return False

            return is_duplicate(root1.left, root2.left) and is_duplicate(root1.right, root2.right)

        node_map = {}
        res = []

        def dfs(root):
            if not root:
                return

            if root.val in node_map:
                add = True
                for node in node_map[root.val]:
                    if is_duplicate(root, node):
                        res.append(root)
                        add = False
                        break
                if add:
                    node_map[root.val].append(root)
            else:
                node_map[root.val] = [root]
            
            dfs(root.left)
            dfs(root.right)

        dfs(root)
        return res
28013