【leetcode❤python】101. Symmetric Tree

时间:2021-12-16 16:01:14
#-*- coding: UTF-8 -*-
# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None class Solution(object):
    resultBool=True
    def isSymmetric(self, root):
        """
        :type root: TreeNode
        :rtype: bool
        """
        if root==None:return True
        if root.left==root.right==None:return True
        elif root.left==None or root.right==None:return False
        if root.left.val==root.right.val:
            self.dnf(root.left,root.right)
            return self.resultBool
        else:
            return False
    
    
    def dnf(self,root1,root2):
        
        if root1==None and root2==None:return
        elif root1==None or root2==None :self.resultBool=False;return
        
        if root1.val!=root2.val:
            self.resultBool=False
           
        self.dnf(root1.left,root2.right)
        self.dnf(root1.right,root2.left)