Inorder Tree Traversal without Recursion

Inorder Tree Traversal without Recursion

Algorithm:
  • Create a Stack
  • stack.push (node)
  • Move node to left i.e. node = node.left
  • Iterate till node is not null or stack is not empty
    • if node is null then
      • node = stack.pop
      • Print node
      • node = node.right
    • Else
      • stack.push (node)
      • node = ndoe.left

Video

Latest Source Code:
Github: BinaryTreeInOrderTraversalWithoutRecursion.java


Output:

               1                               
              / \               
             /   \              
            /     \             
           /       \            
          /         \           
         /           \          
        /             \         
       /               \        
       2               3               
      / \                       
     /   \                      
    /     \                     
   /       \                    
   4       5                       
  /       /                     
 /       /                      
 8       6                       
          \                     
          7                     
                                                                
8 4 2 6 7 5 1 3 
Author: Hrishikesh Mishra