Find Duplicate Subtrees - Problem

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

example_1.py โ€” Basic Tree with Duplicates
$ Input: [1,2,3,4,null,2,4,null,null,4]
โ€บ Output: [2,4]
๐Ÿ’ก Note: Tree has subtrees rooted at nodes with values 2 and 4 that appear multiple times. The subtree '4(null)(null)' appears 3 times, and subtree '2(4(null)(null))(null)' appears 2 times.
example_2.py โ€” Simple Duplicate
$ Input: [2,1,1]
โ€บ Output: [1]
๐Ÿ’ก Note: The subtree consisting of just node 1 appears twice (as left and right children of root). We return one representative.
example_3.py โ€” No Duplicates
$ Input: [2,2,2,3,null,3,null]
โ€บ Output: []
๐Ÿ’ก Note: Although nodes have same values, the subtree structures are different. No duplicate subtrees exist, so return empty list.

Constraints

  • The number of nodes in the tree is in the range [1, 104]
  • -200 โ‰ค Node.val โ‰ค 200

Visualization

Tap to expand
Find Duplicate Subtrees - Visual AlgorithmStep 1: DFS123Post-order traversalStep 2: Serialize"2(null)(null)""3(null)(null)"Create unique fingerprintsStep 3: Hash Lookup"2(null)(null)": 1"2(null)(null)": 2 โœ“Pattern frequency trackingAlgorithm Workflow1. Traverse tree in post-order (children before parent)2. Serialize each subtree: "value(left_serial)(right_serial)"3. Use hash map to count pattern occurrences4. When count reaches 2: DUPLICATE FOUND! Add to result๐ŸŽฏ Key Insight:Instead of comparing trees directly (expensive), we create unique string "fingerprints"that represent each subtree structure. Identical fingerprints = Identical subtrees!Time: O(n) | Space: O(n)
Understanding the Visualization
1
Start DFS Traversal
Begin post-order traversal from root, processing children before parents
2
Serialize Each Subtree
Create unique string fingerprint: 'value(left_pattern)(right_pattern)'
3
Hash Table Lookup
Check if this genetic pattern has been seen before in our catalog
4
Count & Detect
Increment pattern count. When count reaches 2, we found a duplicate family!
Key Takeaway
๐ŸŽฏ Key Insight: Convert each subtree into a unique string fingerprint during post-order traversal. Use hash map to track pattern frequencies - when a pattern appears twice, you've found a duplicate!
Asked in
Google 67 Amazon 45 Microsoft 38 Meta 29
89.2K Views
Medium Frequency
~25 min Avg. Time
2.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen