树型结构数据怎么查找
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:
因为我们只需要检查结构相似性,所以我们不需要检查它们各自的节点值,而是需要检查两者是否具有相同的结构组织。 因此,解决它的简单算法可以是:
If both trees are NULL, then they are structurally similar.
如果两个树都为NULL,则它们在结构上相似。
If one of the two is NULL and other is not NULL, then they are not structurally similar.
如果两者之一是NULL,而另一个不是NULL,则它们在结构上不相似。
If both of the trees are not NULL we recursively check right and left subtree.
如果两个树都不为NULL,则我们递归检查左右子树。
Example:
例:
Fig: Two tree which are structurally identical but are not similar
Image source: wikipedia
图:两棵结构相同但不相似的