简体   繁体   中英

Return Inorder Traversal list

I'm trying to return list from inorder traversal and below is my code. If my input is [1,2,3] and output should be [3,1,2], but I'm returning value none. Can you please suggest what is wrong in my code? Thanks.

def inorderTraversal(currentNode,output=None):

    if output==None:
        output=[]
    if currentNode.left:
        return inorderTraversal(currentNode.left,output)

    return output.append(currentNode.data)

    if currentNode.right:
        return inorderTraversal(currentNode.right,output)

The problem is this line:

return output.append(currentNode.data)

You are returning the value of append but that is None . append changes the list but it does not return the new value of the list. Do this instead.

output.append(currentNode.data)
return output

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