Python 形状同构代码在特定值上失败

Python 形状同构代码在特定值上失败,python,algorithm,python-3.x,graph-algorithm,Python,Algorithm,Python 3.x,Graph Algorithm,我已经编写了一个代码来检查两棵树是否同构: n = int(input()) parent1 = [int(item) for item in input().split()] parent2 = [int(item) for item in input().split()] #Structure to store information about nodes class TreeNode: def __init__(self, data, left=None, right=Non

我已经编写了一个代码来检查两棵树是否同构:

n = int(input())
parent1 = [int(item) for item in input().split()]
parent2 = [int(item) for item in input().split()]


#Structure to store information about nodes
class TreeNode:
    def __init__(self, data, left=None, right=None):
        self.data = data
        self.left = left
        self.right = right

    def add_child(self, node):
        if not self.left:
            self.left = node
        elif not self.right:
            self.right = node

    def __repr__(self):
        return 'TreeNode({self.data!r}, {self.left!r}, {self.right!r})'.format(self=self)



 # Function which converts trees from parent array representation into the usual one.
def construct_tree(parents: list):

    # Put Nodes with corresponding values into the list
    constructed = [TreeNode(i) for i in range(len(parents))]

    root = None
    for i, parent in enumerate(parents):

        # If parent's index = -1, it's the root of the tree
        if parent == -1:
            root = constructed[i]
        else:
            # Tie up current node to corresponding parent
            constructed[parent].add_child(constructed[i])

    return root

def are_isomorphic(T1, T2):
    # Both roots are empty, trees are isomorphic by default

    if len(parent1) != len(parent2):
        return False

    if T1 is None and T2 is None:
        return True
    #if T1.data != T2.data Gives the wrong answer

    # If one of the trees is empty, and the other - isn't, do not bother to check further.
    if T1 is None or T2 is None:
        return False

    # There are two possible cases for n1 and n2 to be isomorphic
    # 1: The subtrees rooted at these nodes haven't been swapped
    # 2: The subtrees rooted at these nodes have been swapped
    return (are_isomorphic(T1.left, T2.left) and are_isomorphic(T1.right, T2.right) or
            are_isomorphic(T1.left, T2.right) and are_isomorphic(T1.right, T2.left))
它实际上为每一对树给出了正确的答案,但以下情况除外:

三烯醇(0,三烯醇(1,三烯醇(3,无,无),三烯醇(4,无, 无),TreeNode(2,无,无)

三烯醇(0,三烯醇(1,三烯醇(3,无,无),无),三烯醇(2, TreeNode(4,无,无,无)

它们不是同构的,但我的代码确定它们是同构的

我画了这些树,并认为这种情况包括在递归过程中。 我试过这个:

if are_isomorphic(T1.left, T2.left) is False:
    return "No"

if are_isomorphic(T1.left, T2.right) is False:
    return "No"

if are_isomorphic(T1.right, T2.left) is False:
    return "No"

if are_isomorphic(T1.right, T2.right) is False:
    return "No"

else:
    return "Yes"
这是:

    if (are_isomorphic(T1.left, T2.left) and are_isomorphic(T1.right, T2.right) is False):
        return "No"

    elif (are_isomorphic(T1.left, T2.right and are_isomorphic(T1.right, T2.left) ) is False):
        return "No"

    else:
        return "Yes"

有人能解释一下我遗漏了什么吗

这不是一个真正的答案,但是没有办法在注释中发布代码。正如我所说,在我看来,您的代码在您给出的示例上生成了正确的结果

#Structure to store information about nodes
class TreeNode:
    def __init__(self, data, left=None, right=None):
        self.data = data
        self.left = left
        self.right = right

    def add_child(self, node):
        if not self.left:
            self.left = node
        elif not self.right:
            self.right = node

def are_isomorphic(T1, T2):
    # Both roots are empty, trees are isomorphic by default

    #if len(parent1) != len(parent2):
        #return False

    if T1 is None and T2 is None:
        return True
    #if T1.data != T2.data Gives the wrong answer

    # If one of the trees is empty, and the other - isn't, do not bother to check further.
    if T1 is None or T2 is None:
        return False

    # There are two possible cases for n1 and n2 to be isomorphic
    # 1: The subtrees rooted at these nodes haven't been swapped
    # 2: The subtrees rooted at these nodes have been swapped
    return (are_isomorphic(T1.left, T2.left) and are_isomorphic(T1.right, T2.right) or
            are_isomorphic(T1.left, T2.right) and are_isomorphic(T1.right, T2.left))

T1=TreeNode(0, TreeNode(1, TreeNode(3, None, None), TreeNode(4, None, None)), TreeNode(2, None, None))

T2=TreeNode(0, TreeNode(1, TreeNode(3, None, None), None), TreeNode(2, TreeNode(4, None, None), None))

print(are_isomorphic(T1, T2))

上面打印的是
False
。如果有问题,一定是在构建树的过程中出现的。

此函数不定义
parent1
parent2
。另外,您的
\uuu len\uu
功能是如何实现的?这可能是问题的原因。
parent1
parent2
是树ESM的父数组,它们是从控制台输入的。我没有包括从父数组构建树的代码,因为它工作正常,与问题无关。len()是一个内置的python函数。是的,但它度量的是什么<代码>[3,无,无]的长度与
[1,2,无]
的长度相同。请发布TreeNodeEdit的定义,编辑您的问题以显示缺少的条件以及您尝试实现这些条件的情况,SO的善良灵魂可能会提供帮助……非常奇怪。谢谢,似乎不仅输出树的正确性,而且它的构建方式都会产生影响。我将自己的代码复制到另一个IDE中,一切都正常工作。我仍然感到惊讶。