查找树是否是其他的子树 [英] Find whether a tree is a subtree of other

查看:114
本文介绍了查找树是否是其他的子树的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有两个二进制树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.

这篇关于查找树是否是其他的子树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆