Given a binary tree, determine whether it is a subtree of another binary tree. A subtree of a tree T is a tree consisting of a node in T and all of its descendants in T.

For example, the second tree is a subtree of the first tree.

Subtree of Binary Tree

Practice this problem

A naive solution would be to check if every subtree rooted at every node in the first tree is identical to the second tree or not. The time complexity of this solution is O(m.n), where n is the size of the first tree and m is the size of the second tree.

 
Also, we know that inorder and preorder traversal, or inorder and postorder traversal identify a tree uniquely. The idea is to store inorder and postorder traversal of both trees in separate arrays. Then for a given binary tree X to be a subset of another binary tree Y, the inorder traversal of X should be a subset of the inorder traversal of Y. Similarly, the postorder traversal of X should be a subset of the postorder traversal of Y. We can also perform preorder instead of the postorder traversal. For example, consider the above trees:

inorder(X) = {4, 2, 5, 1, 6, 3, 7}
inorder(Y) = {6, 3, 7}
 
postorder(X) = {4, 5, 2, 6, 7, 3, 1}
postorder(Y) = {6, 7, 3}

Since inorder(Y) is a subset of inorder(X), and postorder(Y) is a subset of postorder(X), we can say that Y is a subtree of X. The algorithm can be implemented as follows in C++, Java, and Python:

C++


Download  Run Code

Output:

Yes

Java


Download  Run Code

Output:

Yes

Python


Download  Run Code

Output:

Yes

The time complexity of the above solution is O(n2), where n is the total number of nodes in the binary tree. The auxiliary space required by the program is O(n).