forked from AishwaryaBarai/Leetcode-Problems
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path101. Symmetric Tree
34 lines (31 loc) · 1010 Bytes
/
101. Symmetric Tree
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
28
29
30
31
32
33
34
class Solution:
def isSymmetric(self, root: TreeNode) -> bool:
if not root :
return True
left=root.left
right=root.right
return self.isSame(left,right);
def isSame(self,left:TreeNode,right:TreeNode) -> bool:
if(not left and not right):
return True
if(not left or not right):
return False
else:
if left.val != right.val:
return False
return self.isSame(left.right,right.left) and self.isSame(left.left,right.right)
class Solution:
def isSymmetric(self, root: Optional[TreeNode]) -> bool:
q = [root,root]
while q:
t1 = q.pop(0)
t2 = q.pop(0)
if not t1 and not t2:
continue
if not t1 or not t2 or t1.val != t2.val:
return False
q.append(t1.left)
q.append(t2.right)
q.append(t1.right)
q.append(t2.left)
return True