Imagine you're a botanist studying the genetic patterns in a family tree of plants. You've discovered that some branches of the tree have evolved into identical genetic structures - essentially becoming duplicates of each other!
Given the root of a binary tree, your task is to find all duplicate subtrees and return a list containing one representative root node from each group of duplicates. Two subtrees are considered duplicates if they have exactly the same structure with identical node values at corresponding positions.
For example, if you find 3 identical subtrees in the tree, you only need to return the root of one of them. The goal is to identify all the different "genetic patterns" that appear multiple times in your tree.
Input: Root of a binary tree
Output: List of root nodes representing each type of duplicate subtree
Input & Output
Constraints
- The number of nodes in the tree is in the range [1, 104]
- -200 โค Node.val โค 200