-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path28对称的二叉树.py
214 lines (190 loc) · 5.3 KB
/
28对称的二叉树.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#coding= utf-8
#author:Biyoner
class Node(object):
def __init__(self,data,left=None,right= None):
self.data = data
self.left = left
self.right = right
class BinaryTree(object):
def __init__(self):
self.root = None
def preOrderLeft(self,root):
datas = []
if root:
datas.append(root.data)
return datas + self.preOrderLeft(root.left) + self.preOrderLeft(root.right)
else:
datas.append(None)
return datas
def preOrderRight(self,root):
datas = []
if root:
datas.append(root.data)
return datas + self.preOrderRight(root.right) + self.preOrderRight(root.left)
else:
datas.append(None)
return datas
def isSymmetry(tree):
if tree.preOrderLeft(tree.root) == tree.preOrderRight(tree.root):
return True
else:
return False
def Test(name,r, result):
if r == result:
print name+" "+"is passed !"
else:
print name + " " + "is failed !"
# 8
# 6 6
# 5 7 7 5
def test1():
tree = BinaryTree()
tree.root = Node(8)
tree.root.left = Node(6)
tree.root.left.left = Node(5)
tree.root.left.right = Node(7)
tree.root.right = Node(6)
tree.root.right.left = Node(7)
tree.root.right.right = Node(5)
Test("Test1",isSymmetry(tree),True)
# 8
# 6 9
# 5 7 7 5
def test2():
tree = BinaryTree()
tree.root = Node(8)
tree.root.left = Node(6)
tree.root.left.left = Node(5)
tree.root.left.right = Node(7)
tree.root.right = Node(9)
tree.root.right.left = Node(7)
tree.root.right.right = Node(5)
Test("Test2", isSymmetry(tree), False)
# 8
# 6 6
# 5 7 7
def test3():
tree = BinaryTree()
tree.root = Node(8)
tree.root.left = Node(6)
tree.root.left.left = Node(5)
tree.root.left.right = Node(7)
tree.root.right = Node(6)
tree.root.right.left = Node(7)
Test("Test3", isSymmetry(tree), False)
# 5
# / \
# 3 3
# / \
# 4 4
# / \
# 2 2
# / \
# 1 1
def test4():
tree = BinaryTree()
tree.root = Node(5)
tree.root.left = Node(3)
tree.root.left.left = Node(4)
tree.root.left.left.left = Node(2)
tree.root.left.left.left.left = Node(1)
tree.root.right = Node(3)
tree.root.right.right = Node(4)
tree.root.right.right.right = Node(2)
tree.root.right.right.right.right = Node(1)
Test("Test4", isSymmetry(tree), True)
# 5
# / \
# 3 3
# / \
# 4 4
# / \
# 6 2
# / \
# 1 1
def test5():
tree = BinaryTree()
tree.root = Node(5)
tree.root.left = Node(3)
tree.root.left.left = Node(4)
tree.root.left.left.left = Node(6)
tree.root.left.left.left.left = Node(1)
tree.root.right = Node(3)
tree.root.right.right = Node(4)
tree.root.right.right.right = Node(2)
tree.root.right.right.right.right = Node(1)
Test("Test5", isSymmetry(tree), False)
# 5
# / \
# 3 3
# / \
# 4 4
# / \
# 2 2
# \
# 1
def test6():
tree = BinaryTree()
tree.root = Node(5)
tree.root.left = Node(3)
tree.root.left.left = Node(4)
tree.root.left.left.left = Node(2)
tree.root.right = Node(3)
tree.root.right.right = Node(4)
tree.root.right.right.right = Node(2)
tree.root.right.right.right.right = Node(1)
Test("Test6", isSymmetry(tree), False)
def test7():
tree = BinaryTree()
tree.root = Node(1)
Test("Test7", isSymmetry(tree), True)
def test8():
tree = BinaryTree()
Test("Test8", isSymmetry(tree), True)
# 所有结点都有相同的值,树对称
# 5
# / \
# 5 5
# / \
# 5 5
# / \
# 5 5
def test9():
tree = BinaryTree()
tree.root = Node(5)
tree.root.left = Node(5)
tree.root.left.left = Node(5)
tree.root.left.left.left = Node(5)
tree.root.right = Node(5)
tree.root.right.right = Node(5)
tree.root.right.right.right = Node(5)
Test("Test9", isSymmetry(tree), True)
# 所有结点都有相同的值,树不对称
# 5
# / \
# 5 5
# / \
# 5 5
# / /
# 5 5
def test10():
tree = BinaryTree()
tree.root = Node(5)
tree.root.left = Node(5)
tree.root.left.left = Node(5)
tree.root.left.left.left = Node(5)
tree.root.right = Node(5)
tree.root.right.right = Node(5)
tree.root.right.right.left = Node(5)
Test("Test10", isSymmetry(tree), False)
if __name__ == "__main__":
test1()
test2()
test3()
test4()
test5()
test6()
test7()
test8()
test9()
test10()