forked from netsetos/python_code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspiral_traversal
More file actions
33 lines (32 loc) · 879 Bytes
/
spiral_traversal
File metadata and controls
33 lines (32 loc) · 879 Bytes
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
def spiral_traversal(root):
if root is None:
return
s1 = []
s2 = []
s1.append(root)
result = []
curr_res = []
while (s1 or s2):
while (len(s1) > 0):
curr = s1[-1]
curr_res.append(curr.data)
s1.pop()
if (curr.left):
s2.append(curr.left)
if (curr.right):
s2.append(curr.right)
if (len(s1) == 0):
result.append(curr_res)
curr_res = []
while (len(s2) > 0):
curr = s2[-1]
curr_res.append(curr.data)
s2.pop()
if (curr.right):
s1.append(curr.right)
if (curr.left):
s1.append(curr.left)
if (len(s2) == 0):
result.append(curr_res)
curr_res = []
return result