-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathtree_spiral_level_order.py
76 lines (66 loc) · 1.51 KB
/
tree_spiral_level_order.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
#!/usr/bin/python
# Date: 2019-01-01
#
# Description:
# Write a function to print spiral order traversal of a tree.
# Root -> Left to right -> Right to left and so on...
#
# Approach:
# Use 2 stacks, one for left to right and another for left to right. While
# poping elements from one stack add its children to another stack in reverse
# order.
#
# Reference:
# https://www.geeksforgeeks.org/level-order-traversal-in-spiral-form/ - Method 2 (Iterative)
#
# Complexity:
# O(N) Time and space
class Node:
def __init__(self, data):
self.data = data
self.left = None
self.right = None
def print_spiral_order(root):
"""
Prints tree elements in spiral order, first root, next level from left to
right then next level from right to left and so on.
"""
s1 = [root]
s2 = []
while s1 or s2:
while s1:
node = s1.pop()
print(node.data, end=' ')
if node.right:
s2.append(node.right)
if node.left:
s2.append(node.left)
print()
while s2:
node = s2.pop()
print(node.data, end=' ')
if node.left:
s1.append(node.left)
if node.right:
s1.append(node.right)
print()
def main():
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)
# Above tree is
# 1
# 2 3
# 7 6 5 4
print_spiral_order(root)
# Output
# --------
# 1
# 2 3
# 4 5 6 7
if __name__ == '__main__':
main()