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

前端 未结 3 1562
执笔经年
执笔经年 2021-02-08 16:45

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 17:02

    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.

提交回复
热议问题