【剑指Offer】58.对称的二叉树(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
        def isSym(L,R):
            if not L and not R:
                return True
            if L and R and L.val == R.val:
                return isSym(L.left,R.right) and isSym(L.right,R.left)
            return False
        return isSym(pRoot,pRoot)
发布了101 篇原创文章 · 获赞 73 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_36936730/article/details/104769132