zoukankan      html  css  js  c++  java
  • 剑指offer-对称二叉树-树-python

    题目描述

    请实现一个函数,用来判断一颗二叉树是不是对称的。注意,如果一个二叉树同此二叉树的镜像是同样的,定义其为对称的。
     
    # -*- coding:utf-8 -*-
    # class TreeNode:
    #     def __init__(self, x):
    #         self.val = x
    #         self.left = None
    #         self.right = None
    class Solution:
        def isSymmetrical(self, pRoot):
            # write code here
            if not pRoot:
                return True
            return self.compare(pRoot.left,pRoot.right)
        def compare(self,root1,root2):
            if not root1 and not root2:
                return True 
            if not root1 or not root2:
                return False
            if root1.val == root2.val:
                if self.compare(root1.left,root2.right) and self.compare(root1.right,root2.left):
                    return True
            return False

     

  • 相关阅读:
    基本命令
    Unicode Locale
    二进制查看编辑
    java reg
    java util
    js util
    跑到X
    [转]Linux AIO :libaio
    [转]c++ atomic操作
    [转] dpdk笔记
  • 原文地址:https://www.cnblogs.com/ansang/p/11892641.html
Copyright © 2011-2022 走看看