-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path39_isSymmetric.py
More file actions
23 lines (23 loc) · 846 Bytes
/
39_isSymmetric.py
File metadata and controls
23 lines (23 loc) · 846 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
'''
Symmetric Tree
Given the root of a binary tree, check whether it is a mirror of itself (i.e., symmetric around its center).
Input: root = [1,2,2,3,4,4,3]
Output: true
'''
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
from typing import Optional
class Solution:
def isSymmetric(self, root: Optional[TreeNode]) -> bool:
if root == None:return False
def recursion(leftNode,rightNode):
if leftNode==None and rightNode==None:
return True
if leftNode==None or rightNode==None or leftNode.val != rightNode.val:
return False
return recursion(leftNode.left,rightNode.right) and recursion(leftNode.right,rightNode.left)
return recursion(root.left,root.right)