forked from prabhupant/python-ds
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspiral_tree.py
More file actions
56 lines (40 loc) · 1.17 KB
/
spiral_tree.py
File metadata and controls
56 lines (40 loc) · 1.17 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
"""
Do level order traversal of a tree in spiral form
"""
class Node:
def __init__(self, val):
self.val = val
self.left = None
self.right = None
def height(root):
if not root:
return 0
lheight = height(root.left)
rheight = height(root.right)
return 1 + max(lheight, rheight)
def print_spiral(root):
h = height(root)
left_to_right = False
for i in range(1, h+1):
print_level(root, i, left_to_right)
left_to_right = not left_to_right
def print_level(root, level, left_to_right):
if not root:
return
if level == 1:
print(root.val, end=' ')
elif level > 1:
if left_to_right:
print_level(root.left, level-1, left_to_right)
print_level(root.right, level-1, left_to_right)
else:
print_level(root.right, level-1, left_to_right)
print_level(root.left, level-1, left_to_right)
root = Node(1)
root.left = Node(2)
root.right = Node(3)
root.left.left = Node(7)
root.left.right = Node(6)
root.right.left = Node(5)
root.right.right = Node(4)
print_spiral(root)