Inorder Traversal in Binary Tree without recursion in Java
Inorder Traversal in Binary Tree without Recursion
Inorder Traversal in Binary Tree without recursion in Java explains how to traverse a binary tree in Left → Root → Right order using an iterative approach instead of recursion.
In recursive traversal, the system call stack handles function calls. In the non-recursive (iterative) approach, we explicitly use a stack data structure to simulate this behavior.
Inorder Tree Traversal without Recursion in Java
Why Non Recursive Approach?
Recursive Approach Uses:
- Implicit system stack
- Function calls
Iterative Approach Uses:
- Explicit stack
- Better control over execution
- Useful for large inputs
Steps to Find Inorder traversal without Recursion
- Step 1: Create an temporary variable and an empty stack of Node type with initial value null. Push the element value to stack and set temp= temp.left until temp is null .Now stack become , Stack -> 2,3,5.
- Step 2: pop the element from the stack and assign it to temp variable and print it i.e print 2.Now stack become Stack->3,5. pop again and print it i.e. 3.Now stack become , Stack->5.
- step 3: push 4 to stack and make temp null. Stack-> 4,5.
- Step 4: pop 4 from the stack and print it .Now Stack->5. pop 5 from the stack and print it, so stack become Stack->null.
- Step 5: push 7 to stack and make temp null. So Stack become Stack->7.
- Step 6: pop 7 and print it . Now traversal is complete as stack is empty and temp is null.
Therefore the sequence will be printed as 2,3,4,5,7.
Example of Inorder Traversal without Recursion:
Learn DSA
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Method for Inorder Traversal without Recursion in Java
Using Stack (Iterative Inorder Traversal)
Keep pushing left nodes into the stack. Process node when no left child remains and move to right subtree.
Algorithm:
1. Create an empty stack
2. Set current = root
3. While current != null OR stack is not empty:
a. While current != null:
Push current into stack
Move to current.left
b. Pop element from stack
c. Print node.data
d. Move to current.right
Java Code:
import java.util.Stack;
// Node class
class Node {
int data;
Node left, right;
Node(int value) {
data = value;
left = right = null;
}
}
public class InorderWithoutRecursion {
Node root;
// Iterative inorder traversal
void inorder() {
Stack stack = new Stack<>();
Node current = root;
while (current != null || !stack.isEmpty()) {
// Reach the leftmost node
while (current != null) {
stack.push(current);
current = current.left;
}
// Current becomes null here
current = stack.pop();
System.out.print(current.data + " ");
// Visit right subtree
current = current.right;
}
}
public static void main(String[] args) {
InorderWithoutRecursion tree =
new InorderWithoutRecursion();
// Creating tree
tree.root = new Node(10);
tree.root.left = new Node(20);
tree.root.right = new Node(30);
tree.root.left.left = new Node(40);
tree.root.left.right = new Node(50);
System.out.print("Inorder Traversal: ");
tree.inorder();
}
}
Input:
10
/ \
20 30
/ \
40 50
Output:
Inorder Traversal: 40 20 50 10 30
Space Complexity: O(h)
where, h = height of the binary tree
To wrapping up with
Inorder Traversal in Binary Tree without Recursion in Java:
If root is null → no output
Always check stack before pop
Avoid null pointer access
Common Problem with Inorder traversal without recursion:
Forgetting inner while loop (left traversal)
Incorrect stack pop logic
Not moving to right subtree
Infinite loop due to wrong conditions
Frequently Asked Questions
Answer:
It is an iterative method using a stack to traverse a binary tree in Left → Root → Right order.
Answer:
To avoid recursion overhead and handle large trees efficiently.
Answer:
Time complexity is O(n).
Answer:
Space complexity is O(h), where h is the height of the tree.
Answer:
Yes, using Morris Traversal which uses O(1) space.
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Introduction to Trees
Binary Trees
- Binary Tree in Data Structures (Introduction)
- Tree Traversals: Inorder Postorder Preorder : C | C++ | Java
- Inorder Postorder PreOrder Traversals Examples
- Tree Traversal without Recursion
Binary Search Trees
Traversals
- Traversal in Trees
- Tree Traversals: Breadth-First Search (BFS) : C | C++ | Java
- Tree Traversals: Depth First Search (DFS) : C | C++ | Java
- Construct a Binary Tree from Postorder and Inorder
B – Trees
AVL Trees
- AVL Trees
Complete Programs for Trees
- Depth First Traversals – C | C++ | Java
- Level Order Traversal – C | C++ | Java
- Construct Tree from given Inorder and Preorder traversals – C | C++ | Java
- Construct Tree from given Postorder and Inorder traversals – C | C++ | Java
- Construct Tree from given Postorder and Preorder traversal – C | C++ | Java
- Find size of the Binary tree – C | C++ | Java
- Find the height of binary tree – C | C++ | Java
- Find maximum in binary tree – C | C++ | Java
- Check whether two tree are identical- C| C++| Java
- Spiral Order traversal of Tree- C | C++| Java
- Level Order Traversal Line by Line – C | C++| Java
- Hand shaking lemma and some Impotant Tree Properties.
- Check If binary tree if Foldable or not.- C| C++| Java
- check whether tree is Symmetric – C| C++| Java.
- Check for Children-Sum in Binary Tree- C|C++| Java
- Sum of all nodes in Binary Tree- C | C++ | Java
- Lowest Common Ancestor in Binary Tree- C | C++ | Java
Introduction to Trees
Binary Trees
- Binary Tree in Data Structures (Introduction)
- Tree Traversals: Inorder Postorder Preorder : C | C++ | Java
- Inorder Postorder PreOrder Traversals Examples
- Tree Traversal without Recursion
Binary Search Trees
Traversals
- Traversal in Trees
- Tree Traversals: Breadth-First Search (BFS) : C | C++ | Java
- Tree Traversals: Depth First Search (DFS) : C | C++ | Java
- Construct a Binary Tree from Postorder and Inorder
B – Trees
AVL Trees
- AVL Trees
Complete Programs for Trees
- Depth First Traversals – C | C++ | Java
- Level Order Traversal – C | C++ | Java
- Construct Tree from given Inorder and Preorder traversals – C | C++ | Java
- Construct Tree from given Postorder and Inorder traversals – C | C++ | Java
- Construct Tree from given Postorder and Preorder traversal – C | C++ | Java
- Find size of the Binary tree – C | C++ | Java
- Find the height of binary tree – C | C++ | Java
- Find maximum in binary tree – C | C++ | Java
- Check whether two tree are identical- C| C++| Java
- Spiral Order traversal of Tree- C | C++| Java
- Level Order Traversal LIne by Line – C | C++| Java
- Hand shaking lemma and some Impotant Tree Properties.
- Check If binary tree if Foldable or not.- C| C++| Java
- check whether tree is Symmetric C| C++| Java.
- Check for Children-Sum in Binary Tree- C|C++| Java
- Sum of all nodes in Binary Tree- C | C++ | Java
- Lowest Common Ancestor in Binary Tree. C | C++ | Java

Login/Signup to comment