checking subtrees using preorder and inorder strings

后端 未结 4 726
别跟我提以往
别跟我提以往 2020-12-10 16:16

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 <

4条回答
  •  [愿得一人]
    2020-12-10 17:12

    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.

提交回复
热议问题