-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSymmetricTree_Day65.py
More file actions
59 lines (46 loc) · 1.48 KB
/
Copy pathSymmetricTree_Day65.py
File metadata and controls
59 lines (46 loc) · 1.48 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#Brute Approach
# Brute Force
class Solution:
def isSymmetric(self, root: Optional[TreeNode]) -> bool:
arr = []
def inorder(node):
if not node:
arr.append(None)
return
inorder(node.left)
arr.append(node.val)
inorder(node.right)
inorder(root)
return arr == arr[::-1]
#Better Approach
class Solution:
def isSymmetric(self, root: Optional[TreeNode]) -> bool:
if not root:
return True
def isMirror(t1, t2):
if not t1 and not t2:
return True
if not t1 or not t2:
return False
return (t1.val == t2.val) and \
isMirror(t1.left, t2.right) and \
isMirror(t1.right, t2.left)
return isMirror(root.left, root.right)
#Optimal Approach
from collections import deque
class Solution:
def isSymmetric(self, root: Optional[TreeNode]) -> bool:
if not root:
return True
q = deque([(root.left, root.right)])
while q:
t1, t2 = q.popleft()
if not t1 and not t2:
continue
if not t1 or not t2 or t1.val != t2.val:
return False
q.append((t1.left, t2.right))
q.append((t1.right, t2.left))
return True
# Time Complexity : O(N) (each node checked once).
# Space Complexity : O(W) (queue holds nodes, W = max width of tree, worst O(N)).