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

前端 未结 6 910
被撕碎了的回忆
被撕碎了的回忆 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:07

    Na...This approach is not correct.Because, different trees can have same traversal . For instance here in the given example, the tree is

             26
            /   \
          10     3
        /    \     \
      4      6      3
       \
        30
    

    and the candidate subtrees are

    10
    / \
    4 6
    \
    30
    

    and

    30
    / \
    4 10
    \
    6
    

    have the same inorder traversal as 4,30,10,6 but the second one isn't subtree

提交回复
热议问题