Inorder Traversal is a method to traverse a tree such that for each node, you first traverse its left subtree, then visit the node itself, and finally traverse its right subtree.
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.
The main idea is to traverse the tree recursively, starting from the root node, first completely traverse the left subtree, then visit the root node itself, and finally completely traverse the right subtree.
C++
#include<iostream>#include<vector>usingnamespacestd;//Node StructureclassNode{public:intdata;Node*left;Node*right;Node(intx){data=x;left=right=NULL;}};voidinOrder(Node*node,vector<int>&res){if(node==nullptr)return;// Traverse the left subtree firstinOrder(node->left,res);// Visit the current noderes.push_back(node->data);// Traverse the right subtree lastinOrder(node->right,res);}intmain(){// Create binary tree// 1// / \ // 2 3// / \ \ // 4 5 6Node*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);vector<int>res;inOrder(root,res);for(intnode:res)cout<<node<<" ";return0;}
C
#include<stdio.h>#include<stdlib.h>//Node StructurestructNode{intdata;structNode*left;structNode*right;};// Function to create a new nodestructNode*newNode(intx){structNode*node=(structNode*)malloc(sizeof(structNode));node->data=x;node->left=NULL;node->right=NULL;returnnode;}voidinOrder(structNode*node){if(node==NULL)return;// Traverse the left subtree firstinOrder(node->left);// Visit the current nodeprintf("%d ",node->data);// Traverse the right subtree lastinOrder(node->right);}intmain(){// Create binary tree// 1// / \ // 2 3// / \ \ // 4 5 6structNode*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);inOrder(root);printf("\n");return0;}
Java
importjava.util.ArrayList;//Node StructureclassNode{intdata;Nodeleft;Noderight;Node(intx){data=x;left=right=null;}}publicclassGFG{staticvoidinOrder(Nodenode,ArrayList<Integer>res){if(node==null)return;// Traverse the left subtree firstinOrder(node.left,res);// Visit the current noderes.add(node.data);// Traverse the right subtree lastinOrder(node.right,res);}publicstaticvoidmain(String[]args){// Create binary tree// 1// / \// 2 3// / \ \// 4 5 6Noderoot=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);ArrayList<Integer>res=newArrayList<>();inOrder(root,res);for(intnode:res)System.out.print(node+" ");}}
Python
#Node StructureclassNode:def__init__(self,x):self.data=xself.left=Noneself.right=NonedefinOrder(node,res):ifnodeisNone:return# Traverse the left subtree firstinOrder(node.left,res)# Visit the current noderes.append(node.data)# Traverse the right subtree lastinOrder(node.right,res)if__name__=="__main__":# Create binary tree# 1# / \# 2 3# / \ \# 4 5 6root=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)res=[]inOrder(root,res)fornodeinres:print(node,end=" ")
C#
usingSystem;usingSystem.Collections.Generic;//Node StructureclassNode{publicintdata;publicNodeleft;publicNoderight;publicNode(intx){data=x;left=right=null;}}classGFG{staticvoidinOrder(Nodenode,List<int>res){if(node==null)return;// Traverse the left subtree firstinOrder(node.left,res);// Visit the current noderes.Add(node.data);// Traverse the right subtree lastinOrder(node.right,res);}staticvoidMain(string[]args){// Create binary tree// 1// / \// 2 3// / \ \// 4 5 6Noderoot=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);List<int>res=newList<int>();inOrder(root,res);foreach(intnodeinres)Console.Write(node+" ");}}
JavaScript
//Node StructureclassNode{constructor(x){this.data=x;this.left=null;this.right=null;}}functioninOrder(node,res){if(node===null)return;// Traverse the left subtree firstinOrder(node.left,res);// Visit the current noderes.push(node.data);// Traverse the right subtree lastinOrder(node.right,res);}//Driver Code// Create binary tree// 1// / \// 2 3// / \ \// 4 5 6constroot=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);constres=[];inOrder(root,res);console.log(res.join(' '));
Output
4 2 5 1 3 6
Time Complexity: O(n) 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)
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.