---
tags: data_structure_python
---
# Symmetric Tree <img src="https://img.shields.io/badge/-easy-brightgreen">
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
For example, this binary tree [1,2,2,3,4,4,3] is symmetric:
```
1
/ \
2 2
/ \ / \
3 4 4 3
```
But the following [1,2,2,null,3,null,3] is not:
```
1
/ \
2 2
\ \
3 3
```
## Solution
### Solution 1: Depth First Search
```python=
# 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, root1, root2):
if root1 is None and root2 is None:
return True
elif root1 is None or root2 is None:
return False
else:
return ((root1.val == root2.val)
and self.__isSymmetric(root1.left, root2.right)
and self.__isSymmetric(root1.right, root2.left))
def isSymmetric(self, root: TreeNode) -> bool:
return self.__isSymmetric(root, root)
```
### Solution 2: Breadth First Search
```python=
# 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:
stack = []
stack.append(root)
stack.append(root)
while len(stack) > 0:
isNotSkip = True
node1 = stack.pop(0)
node2 = stack.pop(0)
if node1 is None and node2 is None:
isNotSkip = False
elif node1 is None or node2 is None:
return False
elif node1.val != node2.val:
return False
if isNotSkip:
stack.append(node1.left)
stack.append(node2.right)
stack.append(node1.right)
stack.append(node2.left)
return True
```