@TOC请实现一个函数,用来判断一颗二叉树是不是对称的。注意,如果一个二叉树同此二叉树的镜像是同样的,定义其为对称的。(python版本)
款你喜欢的代码片高亮样式,下面展示同样高亮的 代码片
.
// An highlighted block
# -*- 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 is None:
return True
q1 = []
q2 = []
q1.append(pRoot.left)
q2.append(pRoot.right)
while q1 and q2:
left = q1.pop(0)
right = q2.pop(0)
if left is None and right is None:
continue
if left is None or right is None:
return False
if left.val != right.val:
return False
q1.append(left.left)
q1.append(left.right)
q2.append(right.right)
q2.append(right.left)
return True