# Binary Tree PostOrder traversal in java

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

This is 3rd part of java binary tree tutorial.

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

## PostOrder traversal

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

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:

Steps for iterative solution:

1. Create an empty stack `s` and set `currentNode =root`.
2. while `currentNode` is not NULL Do following
1. Push `currentNode 's right child` and then `currentNode` to stack `s`
2. Set `currentNode=currentNode.left`
3. Pop a node from stack `s` and set it to `currentNode`
1. If the popped node has a `right child` and the right child is at top of stack, then `remove` the right child from stack, push the `currentNode` back and set `currentNode` as `currentNode 's right child`.
2. Else print `currentNode's data` and set `currentNode` as NULL.
4. Repeat steps 2 and 3 while stack is not empty.
Lets create java program for postOrder traversal:
Run above program and you will get following output:

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

## 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

• 29 November

### Top 100+ Java coding interview questions

Table of ContentsStringQuestion 1 : How to reverse a String in java? Can you write a program without using any java inbuilt methods?Question 2 : Write a java program to check if two Strings are anagram in java?Question 3 : Write a program to check if String has all unique characters in java?Question 4 : […]

1. Max says:

What happens when the last left node does not have any children?

## Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.