# Binary Tree InOrder traversal in java

If you want to practice data structure and algorithm programs, you can go through 100+ java coding interview questions.

This is 4th part of java binary tree tutorial.

In this post, we will see about InOrder binary tree traversal in java.

## InOrder traversal:

In InOrder traversal,each node is processed between subtrees.In simpler words,Visit left subtree, node and then right subtree.
Steps for InOrder traversal are:
• Traverse the `left subtree` in InOrder.
• `Visit` the node.
• Traverse the `right subtree` in InOrder.

There can be two ways of implementing it

• Recursive
• Iterative

### Recursive solution

Recursive solution is very straight forward.Below diagram will make you understand recursion better.

Code for recursion will be:

### Iterative solution

For recursion, we use implicit stack. So here to convert recursive solution to iterative, we will use explicit stack.
Steps for iterative solution:

1. Create an empty stack `s` and Initialize `currentNode` as root
2. Push the `currentNode` to `s` and set `currentNode = currentNode.left` until `currentNode` is NULL
3. If `currentNode` is NULL and `s` is not empty then
• Pop the top node from stack `s` and print it
•  set `currentNode = currentNode.right`
• go to step 2
4. If stack is empty and currentNode is also null then we are done with it
Lets create java program for InOrder traversal:
Run above program and you will get following output:

Using Recursive solution:
10 20 30 40 50 60 70
————————-
Using Iterative solution:
10 20 30 40 50 60 70

## Java Binary tree tutorial

Please go through java interview programs for more such programs.

import_contacts

## Related Posts

• 18 June

### Maximum Number of Vowels in a Substring of Given Length

Table of ContentsApproach – 1 Generate All Substrings Using substring() MethodApproach – 2 Using Sliding Window Method (Linear Time Solution)Was this post helpful? In this article, we will look at an interesting problem related to the Strings and [Sliding-Window Algorithm](https://java2blog.com/sliding-window-maximum-java/ “Sliding-Window Algorithm”). The problem is : "Given a String we have to Find the Maximum […]

• 04 June

### Search for a range Leetcode – Find first and last position of element in sorted array

Table of ContentsApproach 1 (Using Linear Search)Approach 2 (Using Modified Binary Search-Optimal)Was this post helpful? In this article, we will look into an interesting problem asked in Coding Interviews related to Searching Algorithms. The problem is: Given a Sorted Array, we need to find the first and last position of an element in Sorted array. […]

• 30 April

### Convert Postfix to Infix in Java

Learn about how to convert Postfix to Infix in java.

• 30 April

### Convert Prefix to Postfix in Java

Learn about how to convert Prefix to Postfix in java.

• 16 April