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.

1. Introduction

In this article, we will explore the concept of InOrder traversal in binary trees, focusing on its implementation in Java. In computer science, a binary tree is a foundational data structure, and traversing it is a fundamental technique. Among the various traversal methods, InOrder traversal is widely used due to its applications across a range of algorithms and operations.

2. What is InOrder Traversal?

In InOrder traversal, each node is processed between subtrees. In simpler words, visit the left subtree, node, and then right subtree.

3. Process of InOrder Traversal

  • Traverse the left subtree in InOrder.
  • Visit the node.
  • Traverse the right subtree in InOrder.

4. Implementation

There can be two ways of implementing it

  • Recursive
  • Iterative

4.1 Recursive Solution

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

Code for recursion will be:

The time complexity for recursive approach is O(n), where n is the number of nodes in the binary tree. This is because each node in the tree is visited exactly once.

The space complexity is O(h), where h is the height of the tree. This complexity arises from the use of the call stack to handle recursion. In the worst case (a skewed tree), the height of the tree can become n, making the space complexity O(n).

4.2 Iterative Solution

For recursion, we use an implicit stack. To convert the recursive solution to an iterative one, we will use an explicit stack.
Steps for the iterative solution:

  • Create an empty stack s and initialize currentNode as root.
  • Push the currentNode to s and set currentNode = currentNode.left until currentNode is null.
  • 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.
  • If stack s is empty and currentNode is also null, then the traversal is complete.
The time complexity for iterative approach is O(n), where n is the number of nodes in the binary tree. This is because each node in the tree is visited exactly once.

The space complexity is O(h), where h is the height of the tree. This complexity arises from the use of an explicit stack to keep track of nodes. In the worst case (a skewed tree), the height of the tree can become n, making the space complexity O(n).

5. Complete Java Program

Let’s say our binary tree is:

Binary tree

Here is complete 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

6. Conclusion

In this article, we covered about Binary tree InOrder traversal and its implementation. We have done traversal using two approaches: Iterative and Recursive. We also discussed about time and space complexity for the traversal.
Java Binary tree tutorial

Please go through java interview programs for more such programs.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *