![]() |
VOOZH | about |
Welcome to the daily solutions of our . 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.
Given two binary trees, the task is to find if both of them are identical or not. Return true if they are identical, else return false.
Examples:
Input: 1 1
/ \ / \
2 3 2 3
/ /
4 4
Output: Yes
Explanation: Both trees are identical as the have same root, left and right child.Input: 1 1
/ \ / \
2 3 5 3
/ /
4 4
Output: Trees are not identical
The basic idea behind the Morris traversal approach to solve the problem of checking if two binary trees are identical is to use the Morris traversal algorithm to traverse both trees in-order simultaneously, and compare the nodes visited at each step.
Follow the steps to implement the above idea:
Below is the implementation of the above idea:
Time Complexity: O(N), where N is the number of nodes in the binary tree
Auxiliary Space: O(1)