Inorder Traversal of Binary Tree in Python Last Updated : 22 Feb, 2025 Comments Improve Suggest changes Like Article Like Report Inorder traversal is defined as a type of tree traversal technique which follows the Left-Root-Right pattern, such that:The left subtree is traversed firstThen the root node for that subtree is traversedFinally, the right subtree is traversedConsider the following tree: If we perform an inorder traversal in this binary tree, then the traversal will be as follows:Step-by-step approach:Step 1: The traversal will go from 1 to its left subtree i.e., 2, then from 2 to its left subtree root, i.e., 4. Now 4 has no left subtree, so it will be visited. It also does not have any right subtree. So no more traversal from 4Step 2: As the left subtree of 2 is visited completely, now it read data of node 2 before moving to its right subtree.Step 3: Now the right subtree of 2 will be traversed i.e., move to node 5. For node 5 there is no left subtree, so it gets visited and after that, the traversal comes back because there is no right subtree of node 5.Step 4: As the left subtree of node 1 is, the root itself, i.e., node 1 will be visited.Step 5: Left subtree of node 1 and the node itself is visited. So now the right subtree of 1 will be traversed i.e., move to node 3. As node 3 has no left subtree so it gets visited.Step 6: The left subtree of node 3 and the node itself is visited. So traverse to the right subtree and visit node 6. Now the traversal ends as all the nodes are traversed.So the order of traversal of nodes is 4 -> 2 -> 5 -> 1 -> 3 -> 6.Examples of Inorder TraversalInput: Output: BACExplanation: The Inorder Traversal visits the nodes in the following order: Left, Root, Right. Therefore, we visit the left node B, then the root node A and lastly the right node C.Input : Output: DBEACInput: NULLOutput:Output is empty in this case. Algorithm for Inorder TraversalInorder(root):If root is NULL, then returnInorder (root -> left)Process root (For example, print root’s data)Inorder (root -> right)Python Program to implement Inorder Traversal of Binary Tree Python # Structure of a Binary Tree Node class Node: def __init__(self, v): self.data = v self.left = None self.right = None # Function to print inorder traversal def printInorder(node): if node is None: return # First recur on left subtree printInorder(node.left) # Now deal with the node print(node.data, end=' ') # Then recur on right subtree printInorder(node.right) # Driver code if __name__ == '__main__': root = Node(1) root.left = Node(2) root.right = Node(3) root.left.left = Node(4) root.left.right = Node(5) root.right.right = Node(6) # Function call print("Inorder traversal of binary tree is:") printInorder(root) OutputInorder traversal of binary tree is: 4 2 5 1 3 6 Time Complexity: O(N) where N is the total number of nodes. Because it traverses all the nodes at least once.Auxiliary Space: O(h) where h is the height of the tree. This space is required for recursion calls.In the worst case, h can be the same as N (when the tree is a skewed tree)In the best case, h can be the same as log N (when the tree is a complete treeRelated Posts:Preorder Traversal of Binary Tree in PythonPostorder Traversal of Binary Tree in Python Comment More infoAdvertise with us Next Article Inorder Traversal of Binary Tree in Python B brijkan3mz4 Follow Improve Article Tags : Python Python-DSA Practice Tags : python Similar Reads Preorder Traversal of Binary Tree in Python Preorder traversal is defined as a type of tree traversal that follows the Root-Left-Right policy where:The root node of the subtree is visited first.Then the left subtree is traversed.At last, the right subtree is traversed.Consider the following tree: If we perform a preorder traversal in this bin 3 min read Postorder Traversal of Binary Tree in Python Postorder traversal is defined as a type of tree traversal which follows the Left-Right-Root policy such that for each node:The left subtree is traversed firstThen the right subtree is traversedFinally, the root node of the subtree is traversedConsider the following tree: If we perform a postorder t 3 min read Binary Search Tree In Python A Binary search tree is a binary tree where the values of the left sub-tree are less than the root node and the values of the right sub-tree are greater than the value of the root node. In this article, we will discuss the binary search tree in Python.What is a Binary Search Tree(BST)?A Binary Searc 11 min read Ceil in a Binary Search Tree Python In a Binary Search Tree (BST), the ceiling of a given value is the smallest element in the tree that is greater than or equal to the given value. Finding the ceiling in a BST can be a useful operation when working with ordered data. In this article, we will explore how to find the ceiling in a Binar 3 min read Binary Tree in Python Binary Tree is a non-linear and hierarchical data structure where each node has at most two children referred to as the left child and the right child. The topmost node in a binary tree is called the root, and the bottom-most nodes are called leaves.Introduction to Binary TreeRepresentation of Binar 9 min read Like