_ Binary symmetrical binary tree
Title Description
Please implement a function, a binary tree is used to determine not symmetrical. Note that if a binary image is a binary tree with this same definition as symmetrical.
Problem-solving ideas
just like a bunch of grapes has about two small bunch of grapes, remove the two small bunch of grapes to compare the size of grapes.
Answers
# -*- 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.equal(pRoot.left, pRoot.right)
def equal(self, left, right):
if not left and not right:
return True
if not left or not right:
return False
if left.val !=right.val:
return False
else:
return self.equal(left.left, right.right) and self.equal(left.right, right.left)