# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def isSymmetric(self, root: TreeNode) -> bool:
if root is None:
return True
stack_left, stack_right = [root.left], [root.right]
while stack_left and stack_right:
left, right = stack_left.pop(), stack_right.pop()
if ((left is not None) ^ (right is not None)) or (left and left.val != right.val):
return False
if left:
stack_left.extend([left.left, left.right])
stack_right.extend([right.right, right.left])
return True if not stack_left and not stack_right else False