Find whether a tree is a subtree of other

后端 未结 10 2028
一个人的身影
一个人的身影 2020-12-28 11:57

There are two binary trees T1 and T2 which store character data, duplicates allowed.
How can I find whether T2 is a subtree of T1 ? .
T1 has millions of nodes and

10条回答
  •  独厮守ぢ
    2020-12-28 12:41

    I am not sure, whether my idea is correct. Nevertheless, for your persual.

    1. Conduct a postorder walk in Tree 1 & Tree 2, call it P1 and P2.
    2. Compare P1 & P2. If they are different. The tree is not subtree. Exit.
    3. If they are same, or P1 contained in P2. You can decide which one is sub tree.

提交回复
热议问题