简体   繁体   中英

Inorder traversal Printing Structure

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.

try this :

for i in range(len(inorder)):
  a +=  str(inorder[i])
  if i < len(inorder)-1:
      a += ", "

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:
    ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM