Identical Trees Problem Statement

Given two binary trees with 'N' and 'M' nodes respectively, determine if the two trees are identical. Return true if they are identical, otherwise return false.

Input:

The first line contains an integer T denoting the number of test cases.
The first line of each test case contains elements in level order for the first binary tree, with node values separated by a space. Use -1 for null nodes.
The second line of each test case contains elements in level order for the second binary tree, formatted similarly.

Output:

For each test case, return True if the trees are identical, or False otherwise. Print each result on a new line.

Example:

Input:
1
1 2 3 4 -1 5 6 -1 7 -1 -1 -1 -1 -1 -1
1 2 3 4 -1 5 6 -1 7 -1 -1 -1 -1 -1 -1
Output:
True

Constraints:

  • 1 ≤ T ≤ 10
  • 0 ≤ N ≤ 10^5
  • 0 ≤ M ≤ 10^5
  • 1 ≤ Node Data ≤ 10^9
  • Time limit: 1 sec
Note:
You are not required to print anything; it has already been taken care of in the function implementation.
AnswerBot
4mo

Check if two binary trees are identical by comparing their nodes in level order.

  • Traverse both trees in level order and compare corresponding nodes for equality

  • Use a queue to keep track of nodes to be ...read more

Help your peers!
Select
Add answer anonymously...

Top Software Developer Intern Interview Questions Asked at Microsoft Corporation

Q. What were your responsibilities during your internship?
Q. What is the difference between polymorphism and inheritance?
Q. Given a string, reverse the order of characters using standard data structures a...read more
Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits