I learnt implementing inorder traversal of a binary search tree:
def inorder(root): # root has val, left and right fields
if root==None:
return
inorder(root.left)
print(root.val)
inorder(root.right)
Now, the problem is I do not want console output. I want to get the values in a list. I can't find a way to make the function return a list.
I tried s = [inorder(root)]
but it doesn't work.
So, my question is:
Any way this could be done inside the inorder function, i.e. it should return a list rather than just print values.
Is there some generic way to make recusive functions return data structures instead of just outputting a print to console?