Check if Two Trees are Mirror
Given two arbitrary binary trees, your task is to determine whether these two trees are mirrors of each other.
Explanation:
Two trees are considered mirror of each other if:
- The roots of both the trees are the same.
- The left subtree of the root of the first tree is the mirror of the right subtree of the root of the second tree.
- The right subtree of the root of the first tree is the mirror of the left subtree of the root of the second tree.
Input:
The first line contains an integer ‘T’ representing the number of test cases. For each test case:
The first line contains elements of the first tree in level order format, where node values are separated by spaces. A node with value -1 is considered null.
The second line contains elements of the second tree in level order format, where node values are separated by spaces. A node with value -1 is considered null.
Output:
For each test case, output “true” if the two trees are mirrors of each other; otherwise, output “false” on a separate line for each test case.
Example:
Input:
1
1 2 3 4 -1 5 6 -1 7 -1 -1 -1 -1 -1 -1
1 3 2 6 5 -1 4 -1 -1 -1 -1 7 -1 -1 -1
Constraints:
- 1 <= T <= 80
- 1 <= N, M <= 3000
- -106 ≤ data ≤ 106, where data ≠ -1
Note:
The function does not require printing anything; it is handled internally. Focus on implementing the logic to determine if two trees are mirror of each other.
AnswerBot
20h
Check if two binary trees are mirrors of each other based on specific criteria.
Compare the roots of both trees.
Check if the left subtree of the first tree is the mirror of the right subtree of the sec...read more
Help your peers!
Add answer anonymously...
Popular interview questions of Software Engineer Intern
>
Sterlite Technologies Software Engineer 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