04(July) Duplicate Subtrees

04. Duplicate Subtrees

The problem can be found at the following link: Question Link

Problem Description

Given a binary tree, your task is to find all duplicate subtrees from the given binary tree.

Duplicate Subtree: Two trees are duplicates if they have the same structure with the same node values.

Note: Return the root of each duplicate subtree in the form of a list array. The driver code will print the tree in pre-order traversal in lexicographically increasing order.

Examples:

Input:

        5
       / \
      4   6
     / \
    3   4
       / \
      3   6

Output:

3
6

Explanation: In the above tree, there are two duplicate subtrees: 3 and 6.

My Approach

  1. Helper Function:

    • Define a helper function that serializes each subtree into a string format.

    • Use a map to count the occurrences of each serialized subtree.

  2. Traversal and Serialization:

    • Traverse the tree using post-order traversal to serialize each subtree.

    • If a serialized subtree has been seen exactly once before, add the root of this subtree to the result list.

  3. Return:

    • Return the list of roots of all duplicate subtrees.

Time and Auxiliary Space Complexity

  • Expected Time Complexity: O(n), as we traverse the entire tree once.

  • Expected Auxiliary Space Complexity: O(n), as we use a map to store the serialized subtrees and a list to store the result.

Code (C++)

Code (Java)

Code (Python)

Contribution and Support

For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: Any Questions. Let’s make this learning journey more collaborative!

⭐ If you find this helpful, please give this repository a star! ⭐


📍Visitor Count

Last updated