-1

I was trying to perform Inorder traversal of BST without recursion.

At a particular line of code I was getting error, Code:

Initially I defined out as:

out=[]

elif stack!=[]:
current=stack.pop()
out+=(current.val)
current=current.right

Error: 
Traceback (most recent call last):
  File "main.py", line 224, in 
    Z = obj.inorderTraversal(A)
  File "/tmp/judge/solution.py", line 16, in inorderTraversal
    out+=(current.val)
TypeError: 'int' object is not iterable

Then I changed out+=(current.val) to out.append(current.val), It worked.

Can anyone help me understand this??

Basically, Whats the diff b/w both statements??

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
deepak dash
  • 245
  • 2
  • 11

1 Answers1

0

With append you can specify insert one element and with += you join two lists. See https://docs.python.org/3/tutorial/datastructures.html for more details.

In that case you are using += with an int, correct syntax are out += [current.val] or out.append(current.val) (more readable).