Determine if a binary tree is subtree of another binary tree using pre-order and in-order strings

前端 未结 6 913
被撕碎了的回忆
被撕碎了的回忆 2020-12-09 05:44

I want to find out whether binary tree T2 is a subtree of of binary tree T1. I read that one could build string representations for T2 and T1 using pre-order and in-

6条回答
  •  轮回少年
    2020-12-09 06:08

    I'm reading the same book and doubted its solution as well. I came up with another counter-example that doesn't fall into the potential interpretation that user icepack mentions (of a subtree not necessarily needing all the descendents).

    Consider the following trees

    T2:
      B
     / \
    A   C
    
    T1:
        C
       / \
      B   C
     /
    A
    

    preorder T2: 'BAC'
    preorder T1: 'CBAC'
    inorder T2: 'ABC'
    inorder T1: 'ABCC'

    Again, the T2 strings are substrings of their T1 counterparts and yet T2 is in no way a subtree of T1. Perhaps in the author had ruled out duplicates and specifically mentioned his definition of subtree it could be right, but leaving out that information leaves us with no choice but to consider it an incorrect solution.

提交回复
热议问题