Inorder Tree Traversal without Recursion | non - recursive java program for inorder traversal ; Class containing left and right child of current node and key value ; Class to print the inorder traversal ; traverse the tree ; Reach the left most Node of the curr Node ; place pointer to a tree node on the stack before traversing the node 's left subtree  ; Current must be NULL at this point ; we have visited the node and its left subtree . Now , it ' s ▁ right ▁ ▁ subtree ' s turn ; Driver program to test above functions ; creating a binary tree and entering the nodes
import java . util . Stack ; class Node { int data ; Node left , right ; public Node ( int item ) { data = item ; left = right = null ; } } class BinaryTree { Node root ; void inorder ( ) { if ( root == null ) return ; Stack < Node > s = new Stack < Node > ( ) ; Node curr = root ; while ( curr != null || s . size ( ) > 0 ) { while ( curr != null ) { s . push ( curr ) ; curr = curr . left ; } curr = s . pop ( ) ; System . out . print ( curr . data + " ▁ " ) ; curr = curr . right ; } } public static void main ( String args [ ] ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 1 ) ; tree . root . left = new Node ( 2 ) ; tree . root . right = new Node ( 3 ) ; tree . root . left . left = new Node ( 4 ) ; tree . root . left . right = new Node ( 5 ) ; tree . inorder ( ) ; } }