-
Notifications
You must be signed in to change notification settings - Fork 0
/
crack_4_4.py
44 lines (40 loc) · 1.01 KB
/
crack_4_4.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
class BinaryTree(object):
"""docstring for BinaryTree"""
def __init__(self, data):
self.data = data
self.left = self.right = None
def addTree(tnode, data):
if tnode is None:
tnode = BinaryTree(data)
elif tnode.data > data:
tnode.right = addTree(tnode.right, data)
elif tnode.data < data:
tnode.left = addTree(tnode.left, data)
return tnode
def printTree(tnode):
if tnode is not None:
print tnode.data
printTree(tnode.left)
printTree(tnode.right)
def levelOrder(tnode, n, result, results):
if n == 0 and tnode is not None:
result.append(tnode)
elif len(results[n-1]) != 0:
for node in results[n-1]:
if node.left is not None:
result.append(node.left)
if node.right is not None:
result.append(node.right)
if len(result) != 0:
results.append(result)
levelOrder(tnode, n+1, [], results)
test = [1,3,5,7,9,8,6,4,2,0]
results= []
root = None
for data in range(10):
root = addTree(root, test[data])
levelOrder(root, 0, [], results)
for r in results:
for re in r:
print re.data,
print