7

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.

fo_x86
  • 2,583
  • 1
  • 30
  • 41
  • How about just doing the level order traversal? That's just one instead of two traversing. I don't mean to dismiss the question. Just in case you are simply looking for traversals that work. – kasavbere Jan 12 '13 at 01:01
  • Thanks for the suggestion. In fact the next section gives a more efficient algorithm using a level order traversal. This avoids having to build the pre/inorder strings which does not scale too well as the size of the tree grows. – fo_x86 Jan 12 '13 at 04:33

4 Answers4

7

Consider the two two node trees with A and B as nodes. Tree one has B as root and A as left child. Tree two has A as root and B as right child. The inorder traversals match but the trees differ.

DrC
  • 7,528
  • 1
  • 22
  • 37
  • For completeness, an example where the preorder isn't enough: tree 1 is `1-2-3` with 2 as root, tree 2 is `1-3` with 1 as root. – Daniel Fischer Jan 11 '13 at 23:29
  • 1
    Ah... and here I was not able to come up with a simple example. – fo_x86 Jan 12 '13 at 04:35
  • 1
    If you add punctuation and show the `null` pointers, though, it seems like you can use just one traversal, no? E.g., if you represent each node as either `null` or `( )`, your first tree is `(B A null)` and your second is `(A null B)`, so there's no ambiguity. – Joshua Taylor Aug 19 '13 at 17:43
  • @JoshuaTaylor you would then get problems with 3-3 (left leaning) and 3-3 (right leaning) – Cheng Sep 28 '14 at 19:06
  • @Cheng That's not the case, because of the punctuation. The trees would be (in the **(value left right)** notation): **(3 (3 null null) null)** and **(3 null (3 null null))**. No ambiguity. – Joshua Taylor Sep 28 '14 at 19:10
1

I think you need both if the tree is not a binary search tree but a plain binary tree. Any set of nodes can be a preorder notation. Suppose there is a binary tree a,b,c,d,e,f,g,h and your subtree is cdef. You can create another tree with subtree cde and another subtree fg. There is no way to know the difference.

If it is a binary search tree however you needn't have the inorder.

Incidentally here's an amusing algorithmic problem: given a preorder notation find the number of binary trees that satisfy it.

user1952500
  • 6,611
  • 3
  • 24
  • 37
0

As supplement to user1952500's answer: if it is a binary search tree, either only preorder or only postorder can make it unique, while only inorder can not. For example:

  5
 / \
3   6

inorder: 3-5-6 however, another binary search tree can have the same inorder:

3
 \
  5
   \
    6

Also, I believe preorder+inorder+string_comparison only works to check whether two trees are identical. It fails to check whether a tree is the subtree of another tree. To see an example, refer Determine if a binary tree is subtree of another binary tree using pre-order and in-order strings

Community
  • 1
  • 1
0

a preorder traversal with sentinel to represent null node is well enough. we can use this approach to serialize/deserialize binary tree. it means there is one-to-one mapping between a binary tree and its preorder with sentinel representation.

e230217
  • 1
  • 1