Inorder Traversal17 Mar 2025 | 5 min read In this article, we will discuss the inorder traversal in data structure. If we want to traverse the nodes in ascending order, then we use the inorder traversal. Following are the steps required for the inorder traversal:
Linear data structures such as stack, array, queue, etc., only have one way to traverse the data. But in hierarchical data structures such as tree, there are multiple ways to traverse the data. Here we will discuss another way to traverse the tree data structure, i.e., inorder traversal. There are two approaches used for the inorder traversal:
An inorder traversal technique follows the Left Root Right policy. Here, Left Root Right means that the left subtree of the root node is traversed first, then the root node, and then the right subtree of the root node is traversed. Here, inorder name itself suggests that the root node comes in between the left and the right subtrees. We will discuss the inorder traversal using both recursive and iterative techniques. Let's first start with inorder traversal using recursion. Inorder traversal using recursionExample of inorder traversalNow, let's see an example of inorder traversal. It will be easier to understand the procedure of inorder traversal using an example. ![]() The nodes with yellow color are not visited yet. Now, we will traverse the nodes of the above tree using inorder traversal.
After the completion of inorder traversal, the final output is - {15, 25, 28, 30, 35, 40, 45, 50, 55, 60, 70} Complexity of Inorder traversalThe time complexity of Inorder traversal is O(n), where 'n' is the size of binary tree. Whereas, the space complexity of inorder traversal is O(1), if we do not consider the stack size for function calls. Otherwise, the space complexity of inorder traversal is O(h), where 'h' is the height of tree. Implementation of Inorder traversalNow, let's see the implementation of inorder traversal in different programming languages. Program: Write a program to implement inorder traversal in C language. Output ![]() Program: Write a program to implement inorder traversal in C++. Output ![]() Program: Write a program to implement inorder traversal in C#. Output ![]() Program: Write a program to implement inorder traversal in Java. Output ![]() So, that's all about the article. Hope the article will be helpful and informative to you. Next TopicConvert Infix to Postfix notation |
This article explains implementing merge sort on singly linked lists - finding middle nodes, recursively sorting left/right halves, and merging sorted sub lists. Analyzes time and space complexity. Useful for engineers working with linked lists. Linked lists allow efficient insertion/deletion but can be tricky to sort. Merge...
6 min read
A palindrome is a word that reads the same forwards and backward. To write a palindrome we should make sure that every character of the string has a partner on the opposite side of the string (only those who are the same or their reverse). Approach -...
9 min read
What is a Trie data structure? The word "Trie" is an excerpt from the word "retrieval". Trie is a sorted tree-based data-structure that stores the set of strings. It has the number of pointers equal to the number of characters of the alphabet in each node. It...
12 min read
Introduction Within the larger class of subarray sum problems, the problem is a difficult algorithmic task. The objective is to determine the K-th largest sum among an array's potential contiguous subarrays. This issue has a wide range of applications in fields where it's critical to find...
9 min read
Introduction: A priority queue is a data structure that stores elements with associated priorities and allows for efficient retrieval of the element with the highest (or lowest) priority. While there are various implementations of priority queues, one particularly interesting and flexible approach is to use a doubly...
7 min read
? Linear search and binary search are both methods used to search an element. We have given both of these methods an array and a key-value; all we need to do is search that key in the array. We will return the index value corresponding to that...
17 min read
Problem Statement: We are given a 0-indexed sorted array of integers nums. We can perform the following operation any number of times: Choose two indices, i and j, where i < j, such that nums[i] < nums[j]. Now, delete the elements at indices i and j from...
5 min read
The concept of finding the shortest path is very important in computer science and mathematics. Finding the shortest path between two points, A and B, is a fundamental problem with many applications, ranging from maze navigation to logistics route optimization. Finding the shortest path in a...
20 min read
Fundamental of the Data Structure A data structure is a specialized format of data for arranging and storing data so that any user can easily access and worked within an appropriate data to run a program efficiently. Computer memory data can be organized logically or mathematically, and...
7 min read
Problem Statement We are given an integer array deck where deck[i] represents the number written on the ith card. Partition the cards into one or more groups such that: Each group has exactly x cards where x > 1, and All the cards in one group have the same integer...
5 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India