I am working on a Binary search tree and right now I am working on having my inorder traversal be printed the way I want it to. I mostly figured it out but there is one tiny error in the way in which I want it to come out. Currently it prints out as [ -1, 8, 9, 12, 13, 17, 19, ]. I want to get rid of that extra comma and space at the end so that it looks like this. [ -1, 8, 9, 12, 13, 17, 19 ]. I would appreciate any help I could get. Also any suggestions to make it more efficient is also welcomed.
Asked
Active
Viewed 107 times
2 Answers
0
try this :
for i in range(len(inorder)):
a += str(inorder[i])
if i < len(inorder)-1:
a += ", "

Rich
- 3,781
- 5
- 34
- 56

ashish pal
- 467
- 4
- 10
-
sadly that gives me [ -1, 8912131719 ] – nessa.c Apr 06 '18 at 13:23
0
for i in inorder
will traverse the elements of the array so i != len(inorder)
will compare element to the len of the array not the index location
try
for i in range(len(inorder)):
a += str(inorder[i])
if i != len(inorder) - 1:
...

shahaf
- 4,750
- 2
- 29
- 32