checking subtrees using preorder and inorder strings

后端 未结 4 737
别跟我提以往
别跟我提以往 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

    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

提交回复
热议问题