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: [2]
💡 Note: The tree has two identical subtrees rooted at the children of the root. Both subtrees have structure '2(3(null)(null))(null)', so node 2 should be returned as a duplicate.

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 INPUT 1 2 3 4 2 4 4 Input Array (Level Order) [1,2,3,4,null,2,4,null,null,4] Duplicate subtree (2) Duplicate subtree (4) ALGORITHM (DFS) 1 DFS Traversal Traverse tree post-order 2 Serialize Subtrees Create unique string for each node(4) --> "4,#,#" node(2) --> "2,4,#,#,#" node(3) --> "3,2,4,#,#,#,4,#,#" node(1) --> "1,2,4,#,#,#,3,..." 3 Track in HashMap Count occurrences of each "4,#,#" --> count: 3 "2,4,#,#,#" --> count: 2 ... (other subtrees: 1) 4 Find Duplicates Return nodes with count > 1 FINAL RESULT Duplicate Subtrees Found: Subtree: 4 4 Appears 3 times Subtree: 2 2 4 Appears 2 times OUTPUT [2, 4] OK - Solution Found! 2 duplicate subtrees Time: O(n^2) | Space: O(n^2) (serialization storage) Key Insight: Serialize each subtree using post-order DFS traversal to create a unique string representation. Use a HashMap to count occurrences of each serialized subtree. When a subtree's count reaches exactly 2, add its root to the result list (avoiding duplicates in output). TutorialsPoint - Find Duplicate Subtrees | DFS Approach
Asked in
Google 67 Amazon 45 Microsoft 38 Meta 29
89.3K 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