如何判断二叉树是否对称?

请实现一个函数,用来判断一颗二叉树是不是对称的。注意,如果一个二叉树同此二叉树的镜像是同样的,定义其为对称的。

 

class Node():
    def __init__(self,val):
        self.val = val
        self.lchild = None
        self.rchild = None

    
class Tree():
    def __init__(self,root):
        self.root = root
    
    def add(self,item):
        node = Node(item)
        if not self.root:
            self.root = node
            return
        else:
            quene = [self.root]
            while quene:
                cur_node = quene.pop(0)
                if not cur_node.lchild:
                    cur_node.lchild = node
                    return
                else:
                    quene.append(cur_node.lchild)
                    
                if not cur_node.rchild:
                    cur_node.rchild = node
                    print(222222,node.val)
                    return
                else:
                    quene.append(cur_node.rchild)                  
    
    
#     def seek(self):
#         if not root:
#             return

                 
#         #方法1:广度遍历
#         llist = [self.root.lchild]
#         rlist = [self.root.rchild] 
        
#         lquene = [self.root.lchild]        
#         rquene = [self.root.rchild] 
#         print(rquene)
# #         广度遍历左子树
#         while lquene:
#             cur_node = lquene.pop(0)
#             if cur_node.lchild:
#                 lquene.append(cur_node.lchild)
#                 llist.append(cur_node.lchild)
#             if cur_node.rchild:
#                 lquene.append(cur_node.rchild)
#                 llist.append(cur_node.rchild)
                
#         #广度遍历右子树  
#         while rquene:
#             cur_node = rquene.pop(0)
#             if cur_node.lchild:
#                 rquene.append(cur_node.lchild)
#                 llist.append(cur_node.lchild)
                
#             if cur_node.rchild:
#                 rquene.append(cur_node.rchild)
#                 rlist.append(cur_node.rchild)
                
#         n1 = len(llist)
#         n2 = len(rlist)
#         l1 = [i.val for i in llist]
#         l2 = [i.val for i in rlist]
#         print(l1,l2)
            
#         if n1 != n2:
#             return False
#         for i in range(n1):
#             if llist[i].val != rlist[i].val:
                
#                 return False
#         return True
    
#         
    
    #方法二:深度遍历
    #深度遍历(先序)
    
    def seek(self):
        if not self.root:
            return
        def deep_xian(root,a):
            if root==None:
                return
            else:
                a.append(root)
                deep_xian(root.lchild,a)
                deep_xian(root.rchild,a)
                 
        llist = []
        rlist = []

        deep_xian(root.lchild,llist)
        deep_xian(root.rchild,rlist)
        
        n1 = len(llist)
        n2 = len(rlist)
        
        l1 = [i.val for i in llist]
        l2 = [i.val for i in rlist]
        print(l1,l2)
        if n1 != n2:
            return False

        for i in range(n1):
            if llist[i].val != rlist[i].val:
                return False
        return True
    
            
# 测试
root = Node(1)
etree = Tree(root)




etree.add(2)
etree.add(3)
etree.add(4)

etree.add(5)
etree.add(6)
etree.add(7)
etree.add(8)
etree.add(9)
etree.add(10)
print(etree.seek())

 

posted on 2020-05-28 20:17  仙人小麦  阅读(304)  评论(0编辑  收藏  举报