树型结构数据怎么查找_查找两棵树在结构上是否相同| 数据结构

树型结构数据怎么查找

Solution:

解:

Since we need to check only structural similarity we needn’t check their respective node values, rather we need to check whether both have the same structural organization or not. So the simple algorithm to solve it can be:

因为我们只需要检查结构相似性,所以我们不需要检查它们各自的节点值,而是需要检查两者是否具有相同的结构组织。 因此,解决它的简单算法可以是:

  1. If both trees are NULL, then they are structurally similar.

    如果两个树都为NULL,则它们在结构上相似。

  2. If one of the two is NULL and other is not NULL, then they are not structurally similar.

    如果两者之一是NULL,而另一个不是NULL,则它们在结构上不相似。

  3. If both of the trees are not NULL we recursively check right and left subtree.

    如果两个树都不为NULL,则我们递归检查左右子树。

Example:

例:

structurally identical trees

Fig: Two tree which are structurally identical but are not similar
Image source: wikipedia

图:两棵结构相同但不相似的

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值