Why is inorder and preorder traversal useful for creating an algorithm to decide if T2 is a subtree of T1

前端 未结 3 1408
深忆病人
深忆病人 2021-02-08 16:35

I\'m looking at an interview book and the question is:

You have two very large binary trees: T1, with millions of nodes, and T2

3条回答
  •  无人共我
    2021-02-08 16:54

    I think it is not true. Consider:

    T2:
    
      2
     / \
    1   3
    
    inorder 123 preorder 213
    

    and

    T1:
    
          0
         / \
        3   3
       / \ 
      1   1
     / \ 
    0   2
    
    
    inorder 0123103 preorder 0310213
    

    123 is substring of 0123103, 213 is substring of 0310213, but T2 is not subtree of T1.

提交回复
热议问题