Javatpoint Logo
Javatpoint Logo

Check if a Binary Tree is a Subtree of Another Binary Tree

A tree of binary nodes is a sort of structure for data in which every node may produce a maximum of two children, referred to as both left and right siblings. The offspring belong to nodes, and each node has an opportunity for carrying a value. Binary trees are commonly employed in programming and computer science for various tasks that involve sorting, searching, and data storage.

The following are some essential ideas of binary trees:

  1. Root: In a binary tree, the root is the highest node from which all other nodes are derived.
  2. Parent and Child Nodes: Each node in the binary tree is regarded as the parent of both its right and left children.
  3. Leaf Node: A leaf node is a childless node. It is a node that has no offspring.
  4. Internal Node: An internal node has at least one child.
  5. Depth: The number of edges connecting a node to the root node determines its depth in a binary tree.
  6. Height: The highest depth at which a node in a binary tree may be discovered is its height.

Binary trees may be used for various tasks and have many shapes.

Common binary tree types include the following:

  1. Binary Search Tree (BST): In a BST, a node's left child has values less than its value, and its right child has values larger than its value. It is effective for sorting and searching because of this feature.
  2. Balanced Binary Tree: A binary tree is said to be balanced if there is a maximum of one difference in the heights of each node's left and right subtrees. The Red-Black tree and the AVL tree are two instances of balanced trees.
  3. Complete Binary Tree: A binary tree where all nodes are as far to the left as feasible, and every level-possibly the last-is filled.
  4. Complete Binary Tree: A binary tree where each node has two or zero offspring. Another name for it is a correct binary tree.
  5. A Perfect Binary Tree is one in which every leaf node is at the same level, and every interior node has precisely two offspring.

Critical points of Check if a Binary Tree is a Subtree of Another Binary Tree

  1. A subtree is a tree created by choosing a node from the parent tree and all of its offspring. Stated differently, it is a tiny tree enclosed in an enormous tree.
  2. Traversing the Larger Tree: You must traverse the enormous binary tree to determine if one binary tree is a subtree of another. Pre-order, in-order, and post-order traversals are examples of standard traversal techniques.
  3. Finding Possible Root Nodes: Find possible root nodes for subtrees corresponding to the smaller tree's root while exploring the more enormous tree. The values of the nodes in the two trees may be compared to achieve this.
  4. Subtree Comparison: Conduct a subtree comparison for each possible root node found. Determining if the two binary trees are identical requires going through the smaller binary tree and the subtree rooted at the enormous tree's possible root node.
  5. Node Comparison: Make sure that the values of the nodes match, as well as the importance of any associated children when comparing nodes. Recursive in nature, this comparison keeps going until you either discover a matched subtree or run out of options.
  6. Efficiency Considerations: The traversal order and the subtree comparison technique may affect the solution's efficiency. It is possible to lower the subtree check's time complexity by using effective strategies.

The following actions are to determine whether one binary tree is a subtree of another binary tree:

  1. Navigate depth-first or breadth-first across the enormous binary tree, sometimes called the "main" tree.
  2. Comparing each node found in the main tree to the root node of the smaller binary tree (the "subtree") will reveal patterns.
  3. Call an assist function to compare the two subtrees rooted at these nodes if the nodes match.
  4. One can stop exploring if the helper function returns true, indicating that you have located the subtree.
  5. Return false if the main tree traverse is finished without discovering the subtree.

Checking of Binary Tree is Subtree of Another Binary Tree

One may use a similar method as previously mentioned to determine whether one binary tree in Java is a subtree of another binary tree. Two functions must be implemented: one to determine whether two trees are identical and another to search for the subtree by going through the main tree. This is an implementation in Java:

Java Code


Check if a Binary Tree is a Subtree of Another Binary Tree


we create a TreeNode class corresponding to Java code for displaying the binary tree's elements. The Identical function examines if both trees are identical, whereas the subtree function checks whether a subtree exists in the principal tree. The primary method shows how to use these methods to detect whether the binary tree in question is a subtree within a different binary tree.

How to Tell if a Binary Network is a Subtree of an Additional Binary Tree

A repetitious approach might be utilised to determine if one binary hierarchy is a subtree of another. Here's an instruction manual on how to achieve it as follows:

1. A Recursive Function's Definition:

- Write a recursive function to determine whether a given binary tree is a subtree of another binary tree. This method will be run in the broader tree for every possible root node.

2. Normal Case:

- Return false if the giant tree you are examining is empty (null). A bare tree isn't able to be a subtree of any tree.

- Since an empty tree is regarded as a subtree of any tree, return true if the smaller tree is bare.

3. In contrast, Root Nodes

- [Check whether the root nodes of the smaller and bigger trees are equivalent. If that doesn't happen to the case, return false.

4. Iterative Calls:

- Recursively call the functions for the giant tree's left and right subtrees. This implies that you will ascertain whether the smaller tree is a subtree of the more enormous tree's present left and right subtrees.

5. Combine Findings:

- The left and right subtrees must also be subtrees to establish whether the smaller tree is a subtree of the existing more enormous tree. Return true for the current call if the two recursive calls return true. If not, give back false.

6. Turnaround:

- You will usually execute a pre-order, in-order, or post-order traverse of the enormous tree and call the recursive function for each node found to verify all potential root nodes in the enormous tree.


A typical issue in data structures and computer science is figuring out if one binary tree is a subtree of another binary tree. It entails comparing the two trees' architecture to ensure one tree is contained within the other.

Selecting an effective technique for comparing the subtrees is crucial since, for big trees, the job can become computationally demanding. Many methods, such as identifying patterns and serializing, can speed up the subtree comparative operation. Upgrade to Premium

Finally, determining if a binary tree is a subtree of a different binary tree requires a thorough navigation and comparing approach. The comparison method and traversal order selection can significantly impact the solution's efficiency.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA