Welcome to the daily solving of our PROBLEM OF THE DAY with Yash Dwivedi. We will discuss the entire problem step-by-step and work towards developing an optimized solution. This will not only help you brush up on your concepts of Tree but also build up problem-solving skills.
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 tree in the form of a list array & the driver code will print the tree in pre-order tree traversal in lexicographically increasing order.
Examples:
Input :
1
/ \
2 3
/ / \
4 2 4
/
4
Output: 2 4
4
Explanation: The above tree have two duplicate subtrees.i.e
2
/
4 and 4. Therefore, you need to return the above tree root in the form of a list.
Give the problem a try before going through the video. All the best!!!
Problem Link: https://practice.geeksforgeeks.org/problems/duplicate-subtrees/1