Inorder traversal is a depth-first traversal method that follows this sequence:
Left subtree is visited first.
Root node is processed next.
Right subtree is visited last.
How does Inorder Traversal work?
Key Properties:
If applied to a Binary Search Tree (BST), it returns elements in sorted order.
Ensures that nodes are processed in a hierarchical sequence, making it useful for expression trees and BSTs.
Examples
Input:
Output: 2 1 3 Explanation: The Inorder Traversal visits the nodes in the following order: Left, Root, Right. Therefore, we visit the left node 2, then the root node 1 and lastly the right node 3.
#include<bits/stdc++.h>usingnamespacestd;// Structure of a Binary Tree NodestructNode{intdata;structNode*left,*right;Node(intv){data=v;left=right=nullptr;}};// Function to print inorder traversalvoidprintInorder(structNode*node){if(node==nullptr)return;// First recur on left subtreeprintInorder(node->left);// Now deal with the nodecout<<node->data<<" ";// Then recur on right subtreeprintInorder(node->right);}intmain(){structNode*root=newNode(1);root->left=newNode(2);root->right=newNode(3);root->left->left=newNode(4);root->left->right=newNode(5);root->right->right=newNode(6);printInorder(root);return0;}
C
#include<stdio.h>#include<stdlib.h>// Structure of a Binary Tree NodestructNode{intdata;structNode*left,*right;};// Function to print inorder traversalvoidprintInorder(structNode*node){if(node==NULL)return;// First recur on left subtreeprintInorder(node->left);// Now deal with the nodeprintf("%d ",node->data);// Then recur on right subtreeprintInorder(node->right);}// Function to create a new nodestructNode*newNode(intv){structNode*node=(structNode*)malloc(sizeof(structNode));node->data=v;node->left=node->right=NULL;returnnode;}intmain(){structNode*root=newNode(1);root->left=newNode(2);root->right=newNode(3);root->left->left=newNode(4);root->left->right=newNode(5);root->right->right=newNode(6);printInorder(root);return0;}
Java
importjava.util.*;// Structure of a Binary Tree NodeclassNode{intdata;Nodeleft,right;Node(intv){data=v;left=right=null;}}classGfG{// Function to print inorder traversalpublicstaticvoidprintInorder(Nodenode){if(node==null)return;// First recur on left subtreeprintInorder(node.left);// Now deal with the nodeSystem.out.print(node.data+" ");// Then recur on right subtreeprintInorder(node.right);}publicstaticvoidmain(String[]args){Noderoot=newNode(1);root.left=newNode(2);root.right=newNode(3);root.left.left=newNode(4);root.left.right=newNode(5);root.right.right=newNode(6);printInorder(root);}}
Python
classNode:def__init__(self,v):self.data=vself.left=Noneself.right=None# Function to print inorder traversaldefprintInorder(node):ifnodeisNone:return# First recur on left subtreeprintInorder(node.left)# Now deal with the nodeprint(node.data,end=' ')# Then recur on right subtreeprintInorder(node.right)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)printInorder(root)
C#
usingSystem;// Structure of a Binary Tree NodepublicclassNode{publicintdata;publicNodeleft,right;publicNode(intv){data=v;left=right=null;}}publicclassBinaryTree{// Function to print inorder traversalpublicstaticvoidprintInorder(Nodenode){if(node==null)return;// First recur on left subtreeprintInorder(node.left);// Now deal with the nodeConsole.Write(node.data+" ");// Then recur on right subtreeprintInorder(node.right);}publicstaticvoidMain(){Noderoot=newNode(1);root.left=newNode(2);root.right=newNode(3);root.left.left=newNode(4);root.left.right=newNode(5);root.right.right=newNode(6);printInorder(root);}}
JavaScript
// Structure of a Binary Tree NodeclassNode{constructor(v){this.data=v;this.left=null;this.right=null;}}// Function to print inorder traversalfunctionprintInorder(node){if(node===null){return;}// First recur on left subtreeprintInorder(node.left);// Now deal with the nodeconsole.log(node.data);// Then recur on right subtreeprintInorder(node.right);}constroot=newNode(1);root.left=newNode(2);root.right=newNode(3);root.left.left=newNode(4);root.left.right=newNode(5);root.right.right=newNode(6);printInorder(root);
Output
4 2 5 1 3 6
Time Complexity: O(n), n is the total number of nodes Auxiliary Space: O(h), h is the height of the tree.
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 tree)
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.