Find whether a tree is a subtree of other

后端 未结 10 2027
一个人的身影
一个人的身影 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:37

    Traverse T1. If the current node is equal to the root node of T2, traverse both of the trees (T2 and the current subtree of T1) at the same time. Compare the current node. If they are always equal, T2 is a subtree of T1.

提交回复
热议问题