forked from jadid800/love_babbar_450_dsa_questions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path189_zig_zag_trav.py
33 lines (28 loc) · 910 Bytes
/
189_zig_zag_trav.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
from collections import deque
def zigZagTraversal(root):
label_result=[1]
result=[]
q= deque([root,0])
while(q):
v=q.popleft()
label= q.popleft()
if(label == label_result[0]):
add_result(label_result,result)
label_result.append(v.data)
if(v.left != None):
q.append(v.left)
q.append(label+1)
if(v.right != None):
q.append(v.right)
q.append(label+1)
add_result(label_result,result)
return result
def add_result(label_result, result):
if( (label_result[0]+1)%2 == 1):
for i in range(len(label_result)-1 , 0, -1):
result.append(label_result[i])
else:
for i in range(1,len(label_result)):
result.append(label_result[i])
s= label_result[0]+1
label_result[:]=[s]