43对称的二叉树
程序员文章站
2024-02-13 23:09:34
...
题目
43对称的二叉树
‘’’ 题目描述 请实现一个函数,用来判断一颗二叉树是不是对称的。注意,如果一个二叉树同此二叉树的镜像是同样的,定义其为对称的。 ‘’’
分析
实现
# -*- 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 pRoot == None:
return True
def judgeSym(left, right):
if left == None and right == None:
return True
elif left == None or right == None:
return False
if left.val == right.val:
retLeft = judgeSym(left.left, right.right)
retRight = judgeSym(left.right, right.left)
return retLeft and retRight
else:
return False
return judgeSym(pRoot.left, pRoot.right)
上一篇: 【编程之美】2.2不要被阶乘吓到
下一篇: 十大经典排序算法(Python语言描述)