Welcome to the daily solving of our PROBLEM OF THE DAY with Karan Mashru. 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 Binary Tree but will also help you build up problem-solving skills.
In this problem, we are given, a binary tree, to find out whether it contains a duplicate sub-tree of size two or more, or not.
Note: Two same leaf nodes are not considered as subtree as the size of a leaf node is one.
Example :
Input :
1
/ \
2 3
/ \ \
4 5 2
/ \
4 5
Output: 1
Explanation :
2
/ \
4 5
is the duplicate sub-tree.
Give the problem a try before going through the video. All the best!!!
Problem Link: https://practice.geeksforgeeks.org/problems/duplicate-subtree-in-binary-tree/1
Solution IDE Link: https://ide.geeksforgeeks.org/online-cpp-compiler/f478cf93-68c8-4518-9b1f-d4de3c73bfe2