简体   繁体   English

二叉树问题。 检查相似的形状

[英]Binary Trees question. Checking for similar shape

Hi I'm stuck doing this, not sure how to go about it. 嗨,我坚持这样做,不知道如何去做。

If I have two binary trees, how would I check if the have the same shape? 如果我有两个二叉树,我如何检查它是否具有相同的形状? Data in the nodes doesn't matter, just if the tree structures are equal. 只要树结构相等,节点中的数据无关紧要。

Any ideas on how to solve this problem? 关于如何解决这个问题的任何想法?

You can easily do that with recursion. 您可以通过递归轻松完成此操作。 This following code works because two non-empty trees have the same shape if and only if their respective subtrees have the same shape. 以下代码有效,因为当且仅当它们各自的子树具有相同的形状时,两个非空树具有相同的形状。

boolean equalTrees(Node lhs, Node rhs)
{
    // Empty trees are equal
    if (lhs == null && rhs == null)
        return true;

    // Empty tree is not equal to a non-empty one
    if ((lhs == null && rhs != null)
        || (lhs != null && rhs == null))
        return false;

    // otherwise check recursively
    return equalTrees(lhs.left(), rhs.left())
        && equalTrees(lhs.right(), rhs.right())
}

To check two trees, pass their root nodes to the function above. 要检查两棵树,请将其根节点传递给上面的函数。

equalTrees(tree1.root(), tree2.root())

Two Breadth First Searches in parallel would be one way. 并行的两个广度优先搜索将是一种方式。

http://en.wikipedia.org/wiki/Breadth-first_search http://en.wikipedia.org/wiki/Breadth-first_search

With BFS, you can examine all nodes a particular level of the tree at the same time. 使用BFS,您可以同时检查树的特定级别的所有节点。 That way, if you don't distinguish between right and left children (ie if your tree is considered the same if a node's counterpart has one child, regardless of "right" or "left" child) you can handle that at that time. 这样,如果你不区分右边和左边的孩子(即如果你的树被认为是相同的,如果一个节点的对手有一个孩子,不管“右”或“左”孩子)你可以在那时处理。

If I have two binary trees, how would I check if the have the same shape? 如果我有两个二叉树,我如何检查它是否具有相同的形状?

You will be pleased to know that binary trees have an interesting property: they can be represented as arrays . 您将很高兴知道二叉树具有一个有趣的属性: 它们可以表示为数组 Simply convert each tree to one of these arrays and compare the array contents. 只需将每个树转换为其中一个数组并比较数组内容即可。 Blank cells correspond to left or right children that don't exist, defining the structure of the tree. 空白单元格对应于不存在的左或右子项,定义树的结构。 You want to iterate over both arrays and make sure that each blank cell encountered in one array appears in the other array; 您希望迭代两个数组并确保在一个数组中遇到的每个空单元格出现在另一个数组中; this is O(n). 这是O(n)。

Of course, if the arrays are different sizes, you don't have to do any work at all, since trees with different numbers of nodes can't possibly have identical structure. 当然,如果数组大小不同,则根本不需要做任何工作,因为具有不同数量节点的树不可能具有相同的结构。 From there, you're all done. 从那里开始,你们都完成了。

Walk them in pre-order and in-order with symbolic names instead of the actual data and compare the resulting Strings. 使用符号名称而不是实际数据按预先顺序和按顺序遍历它们,并比较生成的字符串。 Maybe some more input on your data structure would be usefull. 也许您的数据结构上的更多输入将是有用的。

Not java related AFAICT 不是java相关的AFAICT

Just follow the branches, mimicking each move in one tree in the other. 只需按照分支,在另一棵树中模仿每一个动作。 In Python pseudo-code: 在Python伪代码中:

class Tree:
    def __init__(self, value, left=None, right=None):
        self.value = value
        self.left = left
        self.right = right

def same_shape(T1, T2):
    if T1 == None:
        return T2 == None

    if T2 == None:
        return T1 == None

    return same_shape(T1.left, T2.left) and same_shape(T1.right, T2.right)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM