In-order Traversal

For in-order traversal, the parent is considered in between left child and right child. Let's say, a tree with node 3 has children node 1 and node 2. Then, in this traversal, we will print node 1 first, then node 3 and node 2 follows.

E.g. [1, 3, 2]

class Traversal:
    def inOrderTraversal(self, root):
        def fun(root, result):
            if not root:
                return

            fun(root.left, result)
            result.append(root.val)
            fun(root.right, result)
        
        result = []
        fun(root, result)

        return result

Now, go ahead and solve this problem: https://leetcode.com/problems/binary-tree-inorder-traversal

Discussion

2

0