Symmetric Tree
Problem Statement
Given the root of a binary tree, check whether it is a mirror of itself (i.e., symmetric around its center).
Example 1:
Input: root = [1,2,2,3,4,4,3]
Output: true
Example 2:
Input: root = [1,2,2,null,3,null,3]
Output: false
Constraints:
- The number of nodes in the tree is in the range [1, 1000].
- -100 <= Node.val <= 100
Code
Python
class Solution:
def isSymmetric(self, root: Optional[TreeNode]) -> bool:
left_stack = [root.left]
right_stack = [root.right]
right_visited = []
left_visited = []
while len(left_stack) != 0:
curr = left_stack.pop()
if curr:
left_visited.append(curr.val)
left_stack.append(curr.right)
left_stack.append(curr.left)
else:
left_visited.append(None)
while len(right_stack) != 0:
curr = right_stack.pop()
if curr:
right_visited.append(curr.val)
right_stack.append(curr.left)
right_stack.append(curr.right)
else:
right_visited.append(None)
return left_visited == right_visited