Subtree Check
Preorder Traversal
Inorder Traversal
Binary Tree Algorithms
Tree Comparison

checking subtrees using preorder and inorder strings

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

In computer science, tree data structures are fundamental for managing hierarchical data and are frequently leveraged in solving complex problems more efficiently. One such problem involves determining if one tree is a subtree of another. This problem can be succinctly solved using preorder and inorder traversals. Here, we'll delve into the technical explanation of the concept, operational mechanisms, and provide examples to clarify the process.

Understanding Trees and Subtrees

Basics of Trees

A tree is a non-linear hierarchical data structure consisting of nodes connected by edges. It has a root node and leaf nodes, with the root node being the beginning of the structure from which other nodes emerge. Trees can be characterized by their height, depth, and the relationships between parent and child nodes.

Defining a Subtree

A subtree of a tree TT is a tree SS such that SS consists entirely of nodes from TT. Simply put, subtree SS should match exactly one of the node structures within the larger tree TT, including all its descendants from any starting node.

The Role of Traversals

To ascertain if tree SS is a subtree of tree TT, we often leverage tree traversal methods, specifically preorder and inorder traversals.

Preorder Traversal

In a preorder traversal, nodes are recursively visited in the order: Root → Left → Right. For any node, you first process the node data, then recursively traverse the left subtree, followed by the right subtree.

Inorder Traversal

An inorder traversal follows the order: Left → Root → Right. Here, starting from a given node, you first traverse the left subtree, process the node data, and then traverse the right subtree.

Checking Subtrees with Traversal Strings

The core concept of checking if SS is a subtree of TT revolves around comparing traversal strings.

Steps to Verify a Subtree

  1. Generate Traversal Strings: Obtain the preorder and inorder traversal strings for both trees TT and SS.
  2. String Matching: Check if the traversal string of SS (in both preorder and inorder styles) appears as a substring within the respective traversal string of TT.
  3. Confirmation of Subtree: If both preorder and inorder traversal strings of SS match as substrings of TT's respective strings, then SS is guaranteed to be a subtree of TT.

Example

Consider tree TT:

2 3 4 5 6

5 6

  • Preorder Traversal of TT: 1-2-4-3-5-6
  • Preorder Traversal of SS: 3-5-6
  • Inorder Traversal of TT: 4-2-1-5-3-6
  • Inorder Traversal of SS: 5-3-6
  • Duplicated Values: Trees with repeated node values may cause erroneous matches, requiring additional structural verification.
  • Null Nodes: Including representation for null nodes in strings can further ensure accuracy, especially for complete matching scenarios.

Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms