-
Notifications
You must be signed in to change notification settings - Fork 0
/
isSymmetric.py
106 lines (86 loc) · 2.81 KB
/
isSymmetric.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File : isSymmetric.py
@Time : 2024/10/11 14:36:19
@Author : Zihao Zheng
@Email : [email protected]
'''
class TreeNode():
def __init__(self,val=0,left=None,right=None):
self.value=val
self.left=left
self.right=right
import collections
class Solution():
#递归
def compare(self,left,right):
if left ==None and right !=None:return False
elif left!=None and right==None:return False
elif left == None and right==None:return True
elif left.value!=right.value:return False
outside=self.compare(left.left,right.right)
inside=self.compare(left.right,right.left)
isSame=outside&inside
return isSame
def isSymmetric(self,root):
if not None:
return True
return self.compare(root.left,root.right)
#迭代
def isSymmetric(self,root):
if not None:
return True
queue=collections.deque()
queue.append(root.left)
queue.append(root.right)
while queue:
left=queue.popleft()
right=queue.popleft()
if not left and not right:
continue
if not left or not right or left.value !=right.value:
return False
queue.append(left.left)
queue.append(right.right)
queue.append(left.right)
queue.append(right.left)
return True
#层次遍历
# from typing import List
# from collections import deque
# class Solution():
# def levelorder(self,root)->List[List[int]]:
# if not root:
# return []
# queue=deque([root])
# result=[]
# while queue:
# level=[]
# for _ in range(len(queue)):
# cur=queue.popleft()
# level.append(cur)
# if cur.left:
# queue.append(cur.left)
# if cur.right:
# queue.append(cur.right)
# result.append(level)
# return result
def isSymmetric(self,root):
if not root:
return True
queue=collections.deque([root.left,root.right])
while queue:
if len(queue)%2!=0:
return False
level=[]
for _ in range(len(queue)):
cur=queue.popleft
if cur:
level.append(cur.value)
queue.append(cur.left)
queue.append(cur.right)
else:
level.append(None)
if level!=level[::-1]:return False
return True