Subtree of Another Tree Problem Statement
Given two binary trees, T and S, determine whether S is a subtree of T. The tree S should have the same structure and node values as a subtree of T.
Explanation:
A subtree of a tree T is a tree S consisting of a node in T and all of its descendants. The entire tree is a subtree when the subtree corresponds to the root node; otherwise, it's termed a proper subtree.
Input:
The first line of input contains an integer ‘T’ representing the number of test cases. The following lines represent the test cases. Each test case consists of two lines. The first line contains the elements of the first tree in level order, with node values separated by a single space. The second line contains the elements of the second tree in level order, also separated by a single space. Use -1 to denote a null node.
Output:
For each test case, print "true" if tree S is a subtree of tree T; otherwise, print "false". Each result should be on a new line.
Example:
Input:
1
2 3
4 -1 5 6
-1 7 -1 -1 -1 -1
-1 -1
Output:
false
Constraints:
- 1 ≤ T ≤ 100
- 1 ≤ N, M ≤ 1000
- 0 ≤ data ≤ 106 and data ≠ -1
Note: You do not need to print anything; it has already been taken care of.

AnswerBot
4mo
Given two binary trees T and S, determine if S is a subtree of T with the same structure and node values.
Create a function to check if tree S is a subtree of tree T by comparing their structures and n...read more
Help your peers!
Add answer anonymously...
Amazon Software Developer interview questions & answers
A Software Developer was asked 1mo agoQ. What is HTML?
A Software Developer was asked 1mo agoQ. What is MySQL?
A Software Developer was asked 2mo agoQ. Given two strings s and t, return true if they are equal when both are typed int...read more
Popular interview questions of Software Developer
A Software Developer was asked 1mo agoQ1. What is HTML?
A Software Developer was asked 1mo agoQ2. What is MySQL?
A Software Developer was asked 2mo agoQ3. What is the system design for the cart feature in an e-commerce website?
Top HR questions asked in Amazon Software Developer
A Software Developer was asked 1mo agoQ1. Tell me about a time you had to get to the root cause of a problem
A Software Developer was asked 5mo agoQ2. What are the short-term and long-term goals for the team or organization?
A Software Developer was asked 5mo agoQ3. Why do you want to work at Amazon?
Stay ahead in your career. Get AmbitionBox app


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
AmbitionBox Awards
Get AmbitionBox app

