此题目分解成进阶版的判断子树相同即可
from collections import deque
class Solution(object):
def isSymmetric(self, root):
"""
:type root: TreeNode
:rtype: bool
"""
if not root:
return True
def new_trees(root_left,root_right):
if root_left == None and root_right == None:
return True
if root_left == None or root_right ==None:
return False
if root_left.val !=root_right.val:
return False
return all((new_trees(root_left.left,root_right.right),new_trees(root_left.right,root_right.left)))
return new_trees(root.left,root.right)
if not root:
return True
deq = deque([root.left,root.right])
while deq:
root_right = deq.pop()
root_left = deq.pop()
if root_left == None and root_right == None:
continue
if root_left == None or root_right == None:
return False
if root_left.val != root_right.val:
return False
deq.extend([root_left.left,root_right.right,root_left.right,root_right.left])
return True