Given two binary trees, root
and subRoot
, determine if subRoot
is a subtree of root
. A subtree of a tree tree
is any node of tree
and all of its descendants. The goal is to check whether there exists a subtree of root
that
matches subRoot
in both structure and node values.
The core idea behind this solution is to serialize both trees (main tree and subtree) into arrays using preorder
traversal with null
markers for absent children, and then apply the KMP (Knuth-Morris-Pratt) pattern matching
algorithm to check if the serialized form of subRoot
exists as a subarray in that of root
.
This avoids expensive recursive subtree comparisons by transforming the problem into a pattern-matching problem over sequences of values.
-
Serialize Both Trees:
- Use preorder traversal (
node -> left -> right
) with explicitnull
markers to capture both structure and values. - This ensures that different tree shapes with the same values aren't mistakenly matched.
- Use preorder traversal (
-
Apply KMP Algorithm:
- Treat the serialized subtree as the pattern and the serialized main tree as the text.
- Use the KMP string matching algorithm to efficiently search for the pattern in the text.
- The KMP algorithm uses a Longest Prefix Suffix (LPS) array to skip redundant comparisons.
-
Edge Cases:
- If
subRoot
isnull
, it’s trivially a subtree. - If
root
isnull
andsubRoot
is not, return false.
- If
- Serialization:
O(n)
forroot
,O(m)
forsubRoot
, wheren
andm
are the number of nodes. - LPS computation:
O(m)
- KMP pattern matching:
O(n)
- Overall:
O(n + m)
- Serialized arrays:
O(n + m)
- LPS array:
O(m)
- Auxiliary stack and list:
O(h)
in worst-case depth - Overall:
O(n + m)
Aspect | Description |
---|---|
Technique | Tree Serialization + KMP Pattern Matching |
Serialization | Preorder with null markers to preserve structure |
Matching | KMP ensures linear time subtree detection |
Performance | Efficient and robust for large trees |
Trade-off | Extra space usage in exchange for faster lookup |
This approach is both elegant and efficient, leveraging string-matching techniques in a tree context. The use of null
in serialization is crucial to avoid false positives due to structural ambiguity, and KMP ensures linear-time matching —
a big win over naive recursive comparison.