The Wayback Machine - https://web.archive.org/web/20220224234101/https://www.geeksforgeeks.org/check-if-a-binary-tree-is-subtree-of-another-binary-tree-set-3/
Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Check if a Binary tree is Subtree of another Binary tree | Set 3

  • Last Updated : 24 Feb, 2022

Given two binary trees, check if the first tree is a subtree of the second one. 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.

 Examples:

Input:            Tree-T                                        Tree-S
                           1                                               3
                        /    \                                           /       
                     2        3                                      6
                  /   \      /
               4      5   6
Output: Yes
Explanation:
Height of a Tree-S is -2
In the Tree-T the nodes with height 2 are -{2 ,3}
In the nodes {2,3} the nodes which satisfy the identical subtree with tree-S are {3}
So, there exists a identical subtree i.e tree-S in the tree-T

Input:  Tree-T                                       Tree-S
              26                                              10
            /   \                                            /    \
        10     3                                        4       6
      /    \      \                                       \
   4       6      3                                     30
    \
    30 

 

Naive Approach: The naive approach is discussed in Set-1 of this problem.

Efficient Approach: The efficient approach based on uniquely identifying a tree from their inorder and preorder/postorder traversal is discussed in Set-2 of this problem.

Alternative Efficient Approach: The approach is to find all nodes at the same height as the height of Tree-S in Tree-T and then compare.

  • Initially calculate the height of the given subtree(Tree -S).
  • In the next step, find all the nodes from Tree-T which are having height as the height of the Tree-S, and store their address.
  • Then from every node, we stored we check if that is an identical subtree or not.
  • In this approach, there is no need to check for all the nodes whether it is an identical subtree or not as we have height as a parameter at which actually identical subtree validation must be performed.

Below is the implementation of the above.

C++




// C++ program to check if a binary tree
// Is subtree of another binary tree
  
#include <bits/stdc++.h>
using namespace std;
  
// Structure of a Binary Tree Node
struct Node {
    int data;
    struct Node *left, *right;
};
  
// Utility function to
// Create a new tree node
Node* newNode(int data)
{
    Node* temp = new Node;
    temp->data = data;
    temp->left = temp->right = NULL;
    return temp;
}
  
// Function to calculate
// The height of the tree
int height_of_tree(Node* root)
{
    if (!root)
        return 0;
  
    // Calculate the left subtree
    // And right subtree height
    int left = height_of_tree(root->left);
    int right = height_of_tree(root->right);
    return 1 + max(left, right);
}
  
// Function to check
// It is a subtree or not
bool CheckSubTree(Node* T, Node* S)
{
    if (S == NULL && T == NULL)
        return true;
  
    if (T == NULL || S == NULL)
        return false;
  
    if (T->data != S->data)
        return false;
  
    return CheckSubTree(T->left, S->left)
           && CheckSubTree(T->right, S->right);
}
  
// Function to extract the
// nodes of height of the subtree
// i.e tree-S from the parent
// tree(T-tree) using the height of the tree
int subtree_height(Node* root, 
                   vector<Node*>& nodes, 
                   int h)
{
    if (root == NULL)
        return 0;
  
    else {
  
        // Calculating the height
        // Of the tree at each node
        int left = subtree_height(root->left, 
                                  nodes, h);
  
        int right = subtree_height(root->right, 
                                   nodes, h);
  
        int height = max(left, right) + 1;
  
        // If height equals to height
        // of the subtree then store it
        if (height == h)
            nodes.push_back(root);
  
        return height;
    }
}
  
// Function to check if it is a subtree
bool isSubTree(Node* T, Node* S)
{
    if (T == NULL && S == NULL)
        return true;
  
    if (T == NULL || S == NULL)
        return false;
  
    // Calling the height_of_tree
    // Function to calculate
    // The height of subtree-S
    int h = height_of_tree(S);
    vector<Node*> nodes;
  
    // Calling the subtree_height
    // To extract all the nodes
    // Of height of subtree(S) i.e height-h
    int h1 = subtree_height(T, nodes, h);
  
    bool result = false;
    int n = nodes.size();
  
    for (int i = 0; i < n; i++) {
  
        // If the data of the
        // node of tree-T at height-h
        // is equal to the data
        // of root of subtree-S
        // then check if is a subtree
        // of the parent tree or not
        if (nodes[i]->data == S->data)
            result = CheckSubTree(nodes[i], S);
  
        // If any node is satisfying
        // the CheckSubTree then return true
        if (result)
            return true;
    }
    // If no node is satisfying
    // the subtree the return false
    return false;
}
  
// Driver program
int main()
{
    // Create binary tree T in above diagram
    Node* T = newNode(1);
    T->left = newNode(2);
    T->right = newNode(3);
    T->left->left = newNode(4);
    T->left->right = newNode(5);
    T->right->left = newNode(6);
  
    // Create binary tree S shown in diagram
    Node* S = newNode(3);
    S->left = newNode(6);
  
    // Lets us call the function
    // isSubTree() which checks
    // that the given Tree -S is a subtree
    // of tree -T(parent tree)
    if (isSubTree(T, S))
        cout << "Yes" << endl;
    else
        cout << "No" << endl;
    return 0;
}
Output
Yes

Time Complexity: O(N)
Auxiliary Space: O( 2H ) where H is the height of Tree-T


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!