forked from keon/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 1
/
bintree2list.py
49 lines (44 loc) · 1.03 KB
/
bintree2list.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
class Node():
def __init__(self, val = 0):
self.val = val
self.left = None
self.right = None
def bintree2list(root):
"""
type root: root class
"""
if not root:
return root
root = bintree2list_util(root)
while root.left:
root = root.left
return root
def bintree2list_util(root):
if not root:
return root
if root.left:
left = bintree2list_util(root.left)
while left.right:
left = left.right
left.right = root
root.left = left
if root.right:
right = bintree2list_util(root.right)
while right.left:
right = right.left
right.left = root
root.right = right
return root
def print_tree(root):
while root:
print(root.val)
root = root.right
tree = Node(10)
tree.left = Node(12)
tree.right = Node(15)
tree.left.left = Node(25)
tree.left.left.right = Node(100)
tree.left.right = Node(30)
tree.right.left = Node(36)
head = bintree2list(tree)
print_tree(head)