Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inorder Binary Tree Traversal (using Python)

I am trying to perform an inorder traversal of a tree. The code itself feels right, except it is not working properly. I have a feeling it has to either do with the if condition, how append works in python, or something perhaps with return. This works correctly if I use print instead of return, I think, but I want to be able to use return and still get the correct answer. For example, for the tree [1,None,2,3], my code returns [1] which is clearly incorrect.

Additionally is it possible to solve this problem using list comprehension? If so, any sample code would be greatly appreciated.

Here is my code:

    class Solution(object):
        def inorderTraversal(self, root):
            res = []
            if root:
                self.inorderTraversal(root.left)
                res.append(root.val)
                self.inorderTraversal(root.right)
            return res

Also before marking this as a duplicate, I know in order traversals have been asked on Stackoverflow (plenty of times), but none of them helped me understand why my understanding is wrong. I would be so grateful if someone helped me learn how to correct my approach versus simply posting another link without explanation. Thank you so much!

like image 244
Jane Sully Avatar asked Jan 12 '17 20:01

Jane Sully


People also ask

How do you write inorder traversal in Python?

In-order Traversal Then we create a insert function to add data to the tree. Finally the Inorder traversal logic is implemented by creating an empty list and adding the left node first followed by the root or parent node. At last the left node is added to complete the Inorder traversal.

How do you traverse through a tree in Python?

Now, there are 3 main methods for tree traversal in python with recursion using DFS which are: Inorder Traversal (left, root, right) Preorder Traversal (root, left, right) Postorder Traversal (left, right, root)

What is traverse in Python?

Lists are basically equal to arrays in other languages. However, they do have the extra benefit of being dynamic in size. In Python, the list is a kind of container in Data structures. It comes in use for storing numerous data at the same time.


1 Answers

The reason this doesn't work is that res only has the value of the first node you give it appended to it; each time you recursively recall the function, it just makes a new res. It is a simple fix though, as follows:

class Solution(object):
    def inorderTraversal(self, root):
        res = []
        if root:
            res = self.inorderTraversal(root.left) 
            res.append(root.val)
            res = res + self.inorderTraversal(root.right)
        return res

In this, it returns the left branch, the value, and then the right. This can be done much more briefly as follows:

class Solution(object):
    def inorderTraversal(self, root):
        return (self.inorderTraversal(root.left) + [root.val] + self.inorderTraversal(root.right)) if root else []
like image 190
Benedict Randall Shaw Avatar answered Nov 14 '22 19:11

Benedict Randall Shaw