-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0101_Symmetric_Tree.py
More file actions
27 lines (26 loc) · 942 Bytes
/
0101_Symmetric_Tree.py
File metadata and controls
27 lines (26 loc) · 942 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# 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
class Solution:
def isSymmetric(self, root: Optional[TreeNode]) -> bool:
def helper(_leftRoot, _rightRoot):
if not(_leftRoot) and not(_rightRoot):
return True
if not(_leftRoot and _rightRoot):
return False
if _leftRoot.val != _rightRoot.val:
return False
leftBool = helper(_leftRoot.left, _rightRoot.right)
if not leftBool:
return False
rightBool = helper(_rightRoot.left, _leftRoot.right)
if not rightBool:
return False
return True
if not(root.left or root.right):
return True
else:
return helper(root.left, root.right)