Isomorphic Trees Problem Statement
You are provided with two arbitrary binary trees. Determine if both trees are isomorphic.
Explanation:
Two binary trees are considered isomorphic if one tree can be transformed into the other by repeatedly performing the following operation:
Choose a node of the tree, swap its left and right subtree, meaning the left subtree becomes the right one, and vice versa.
Input:
The input consists of multiple test cases. Each test case begins with:
The first integer 't' representing the number of test cases.
For each test case, the first line contains the node values of the first tree in level order (use -1 for NULL nodes).
The second line contains the node values of the second tree in level order (use -1 for NULL nodes).
Output:
For each test case, return "yes" if both trees are isomorphic, "no" otherwise.
Example:
Consider the binary tree shown in the image:
The input of this tree would be: 20 10 35 5 15 30 42 -1 -1 -1 13 -1 -1 -1 -1 -1 -1
Constraints:
1 <= T <= 100
0 <= N <= 103
1 <= nodeValue <= 106
- Time Limit: 1 sec
Note:
1. A binary tree is a tree where each node has up to two children.
2. An empty tree can be deemed isomorphic with another empty tree.
3. Perform the given operation on any node at any level.
You need not print anything, simply implement the function to return the result.
AnswerBot
2d
Determine if two binary trees are isomorphic by swapping left and right subtrees.
Check if both trees are empty, return 'yes' if they are
Perform the swap operation on any node at any level
Compare the s...read more
Help your peers!
Add answer anonymously...
Popular interview questions of Software Developer Intern
>
Innovaccer Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app