Symmetric binary prove safety offer- - Tree -python

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.
 
# -*- 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

 

Guess you like

Origin www.cnblogs.com/ansang/p/11892641.html