A book I'm reading claims that one way to check whether a binary tree B
is a subtree of a binary tree A
is to build the inorder
and preorder
strings (strings that represent the inorder and preorder traversal of each tree) of both trees, and check whether inorder_B
is a substring of inorder_A
and preorder_B
is a substring of preorder_A
. Note that it claims that you have to check substring match on both the inorder and preorder strings.
Is it really necessary to check for a substring match on both the inorder and preorder strings? Wouldn't it suffice to check either? Could someone provide an example to prove me wrong (i.e. prove the claim in the book right)? I couldn't come up with an example where two trees were unequal but the preorder or inorder strings match.