I'm looking at an interview book and the question is:
You have two very large binary trees:
T1
, with millions of nodes, andT2
, with hundreds of nodes. Create an algorithm to decide ifT2
is a subtree ofT1
.
The authors mentions this as a possible solution:
Note that the problem here specifies that
T1
has millions of nodes—this means that we should be careful of how much space we use. Let’s say, for example,T1
has 10 million nodes—this means that the data alone is about40 mb
. We could create a string representing the inorder and preorder traversals. IfT2
’s preorder traversal is a substring ofT1
’s preorder traversal, andT2
’s inorder traversal is a substring ofT1
’s inorder traversal, thenT2
is a substring ofT1
.
I'm not quite sure the logic behind as to why if these are true:
T2-preorder-traversal-string
is a substring ofT1-preorder-traversal-string
T2-inorder-traversal-string
is a substring ofT1-inorder-traversal-string
That T2
must be a substring (although I assume the author means subtree) of T1
. Can I get an explanation to this logic?
EDIT: User BartoszMarcinkowski brings up a good point. Assume both trees have no duplicate nodes.