判断是否对称二叉树

在这里插入图片描述
这题不是简单的后序遍历,注意是整颗二叉树的对称,而不是单个节点的对称。
请实现一个函数,用来判断一棵二叉树是不是对称的。如果一棵二叉树和它的镜像一样,那么它是对称的。

一共用了两个方法,第一个比较垃圾。还要空间复杂度存储,相当于两个遍历,最后比对;第二个方法直接比对两棵树,相当于放入两个对应的节点进行比对:
面试可以先法一再法二
方法1:

class Solution:
    def isSymmetric(self , root ):
        record1 = []
        record2 = []
        
        def last_order1(root):
            if not root:
                record1.append(float("-inf"))
                return 
            last_order1(root.left)
            last_order1(root.right)
            record1.append(root.val)
            
        def last_order2(root):
            if not root:
                record2.append(float("-inf"))
                return 
            last_order2(root.right)
            last_order2(root.left)
            record2.append(root.val)
            
        last_order1(root)
        last_order2(root)
        if record1 == record2:
            return True
        else:
            return False

方法二:

class Solution:
    def isSymmetric(self , root ):
        if not root:
            return True
        def compare(left, right):
            if not left and not right:
                return True
            elif not left:
                return False
            elif not right:
                return False
            else:
                if left.val == right.val:
                    res1 = compare(left.left, right.right)
                    res2 = compare(left.right, right.left)
                    if res1 and res2:
                        return True
                    else:
                        return False
                else:
                    return False
        return compare(root.left, root.right)

非递归实现


class Solution:
    def isSymmetric(self , root ):
        record = []
        if not root:
            return True
        record.append(root.left)
        record.append(root.right)
        while record:
            res1 = record.pop()
            res2 = record.pop()
            if not res1 and not res2:
                continue
            elif not res1:
                return False
            elif not res2:
                return False
            elif res1.val != res2.val:
                return False
            record.append(res1.left)
            record.append(res2.right)
            record.append(res1.right)
            record.append(res2.left)
        return True

归根结底还是判断左子树和右子树是不是对称的,即判断左子树的右结点是不是等于右子树的左结点。

class Solution(object):def isSymmetric(self, root):
        if not root :
            return True
        def equal(root1,root2):
            if root1 == None and root2 == None:
                return True
            if not root1 or not root2:
                return False
            if root1.val != root2.val:
                return False
            return equal(root1.left,root2.right) and equal(root1.right,root2.left)
        return equal(root.right,root.left)

方法二比较暴力,直接镜像之后逐个对比。


class Solution(object):
    def mirrortree(self, root):
        if not root:
            return
        root.right, root.left = root.left, root.right
        self.mirrortree(root.right)
        self.mirrortree(root.left)
        return root

    def isequal(self, root, root1):
        if root1 == None and root == None:
            return True
        if not root1:
            return False
        if not root:
            return False
        if root.val != root1.val:
            return False
        return self.isequal(root.left, root1.left) and self.isequal(root.right, root1.right)

    def copy(self, head):
        if not head:
            return
        head1 = TreeNode(1)
        head1.val = head.val
        head1.left = self.copy(head.left)
        head1.right = self.copy(head.right)
        return head1

    def isSymmetric(self, root):
        if not root:
            return True
        a = []
        root1 = self.copy(root)
        root1 = self.mirrortree(root1)
        return self.isequal(root, root1)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值