Close
Topic list
Subtree of Another Tree
EASY
10 mins
Other topics
Problem solved
Skill meter
Strings
-
-
Matrices (2D Arrays)
-
-
-
-
Sorting
-
-
Binary Search
-
-
Stacks & Queues
-
-
Trees
-
-
Graph
-
-
Dynamic Programming
-
-
Greedy
-
-
Tries
-
-
Arrays
-
-
SQL
-
-
Binary Search Trees
-
-
Heap
-
-
Bit Manipulation
-
-
Solve problems & track your progress
Checkout your overall progress in every topic here
Become
Sensei
in DSA topics
Open the topic and solve more problems associated with it to improve your skills
Check out the skill meter for every topic
See how many problems you are left with to solve for cracking any stage. Score more than zero to get your progress counted.

# Subtree of Another Tree

Contributed by
Arindam Majumder
Easy
0/40
Avg time to solve 10 mins
Success Rate 90 %
Share

## Problem Statement

#### A subtree of a tree T is a tree S consisting of a node in T and all of its descendants in T. The subtree corresponding to the root node is the entire tree; the subtree corresponding to any other node is called a proper subtree.

Detailed explanation ( Input/output format, Notes, Constraints, Images )
##### Sample Input 1:
``````2
1 2 3 4 5 6 7 -1 -1 -1 -1 -1 -1 -1 -1
3 6 7 -1 -1 -1 -1
3 4 5 1 2 -1 -1 -1 -1 0 -1 -1 -1
4 1 2 -1 -1 -1 -1
``````
##### Sample Output 1:
``````true
false
``````
##### Explanation of Sample Input 1:

``````For the first test case, the second tree is a subtree of the first tree as the second tree has exactly the same structure and node values with a subtree of the first tree.

For the second test case, the second tree is not a subtree of the first tree because node 2 in the first tree has 1 child having a value 0, whereas node 2 in the second tree doesn’t have any child.
``````
##### Sample Input 2:
``````2
26 10 3 4 6 -1 3 -1 30 -1 -1 -1 -1 -1 -1
10 4 6 -1 30 -1 -1 -1 -1
3 4 5 1 2 -1 -1 -1 -1 -1 -1
4 1 -1 -1 -1
``````
##### Sample Output 2:
``````true
false
``````
Auto
Console