Given a binary tree, check if the tree can be folded or not. A tree can be folded if left and right subtrees of the tree are structure wise same. An empty tree is considered as foldable.
Consider the below trees:
(a) and (b) can be folded.
(c) and (d) cannot be folded.
Input: 10 / \ 7 15 / \ / \ N 9 11 N Output:Yes
Input: 10 / \ 7 15 / \ / \ 5 N 11 N Output: No
The task is to complete the function isFoldable() that takes root of the tree as input and returns true or false depending upon whether the tree is foldable or not.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(Height of the Tree).
1 <= n <= 103
1 <= data of node <= 104
If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.