查找树是否是其他的子树子树、其他的

由网友(爱你爱的像狗)分享简介:有两个二进制树T1和T2用来存储字符数据,允许重复。我如何才能找到T2是否是T1的子树? 。照片T1拥有数以百万计的节点和T2拥有上百个节点。There are two binary trees T1 and T2 which store character data, duplicates allowed.How...

有两个二进制树T1和T2用来存储字符数据,允许重复。  我如何才能找到T2是否是T1的子树? 。照片  T1拥有数以百万计的节点和T2拥有上百个节点。

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 T2 has hundreds of nodes.

推荐答案

导线T1。如果当前节点是等于T2的根节点,同时遍历两者树木(T2和T1的当前子树)的。比较当前节点。如果它们总是相等,T2为T1的一个子树。

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.

阅读全文

相关推荐

最新文章