首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >形状同构代码在特定值上失败

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

Stack Overflow用户
提问于 2017-05-14 12:38:02
回答 1查看 38关注 0票数 0

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

代码语言:javascript
复制
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))

它对每一棵树都给出了正确的答案,但以下几种除外:

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

它们不是同构的,但我的代码决定了它们是同构的。

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

代码语言:javascript
复制
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"

这是:

代码语言:javascript
复制
    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"

有人能解释一下我错过了什么吗?

EN

回答 1

Stack Overflow用户

发布于 2017-05-14 16:20:28

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

代码语言:javascript
复制
#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。如果有问题,一定是在你构建树的过程中。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43963981

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档