-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy path101.py
42 lines (39 loc) · 1.52 KB
/
101.py
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
35
36
37
38
39
40
41
42
__________________________________________________________________________________________________
sample 24 ms submission
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def isSymmetric(self, root: TreeNode) -> bool:
return root is None or self.isMirror(root.left,root.right)
def isMirror(self,l,r):
if l is None and r is None:
return True
elif l is None or r is None:
return None
else:
return l.val==r.val and self.isMirror(l.left,r.right) and self.isMirror(l.right,r.left)
__________________________________________________________________________________________________
sample 12960 kb submission
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def isMirror(self, node1,node2):
if(not node1 and not node2):
return True
if(not node1 or not node2):
return False
# if(node1.val != node2.val):
# return False
return (node1.val == node2.val) and self.isMirror(node1.left,node2.right) and self.isMirror(node1.right,node2.left)
def isSymmetric(self, root: TreeNode) -> bool:
return self.isMirror(root, root)
# print(root)
__________________________________________________________________________________________________