Skip to content

Commit 37ecd88

Browse files
authored
Create Symmetric Tree - Leetcode 101.py
1 parent 95ae29e commit 37ecd88

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

Symmetric Tree - Leetcode 101.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, val=0, left=None, right=None):
4+
# self.val = val
5+
# self.left = left
6+
# self.right = right
7+
class Solution:
8+
def isSymmetric(self, root: Optional[TreeNode]) -> bool:
9+
def same(root1, root2):
10+
if not root1 and not root2:
11+
return True
12+
13+
if not root1 or not root2:
14+
return False
15+
16+
if root1.val != root2.val:
17+
return False
18+
19+
return same(root1.left, root2.right) and \
20+
same(root1.right, root2.left)
21+
22+
return same(root, root)

0 commit comments

Comments
 (0)