Postorder traversal of Binary Tree without recursion and without stack | JAVA program or postorder traversal ; A binary tree node has data , pointer to left child and a pointer to right child ; Helper function that allocates a new node with the given data and null left and right pointers . ; Visited left subtree ; Visited right subtree ; Print node ; Driver program to test above functions
import java . util . * ; class Node { int data ; Node left , right ; Node ( int data ) { this . data = data ; this . left = this . right = null ; } } ; class GFG { Node root ; void postorder ( Node head ) { Node temp = root ; HashSet < Node > visited = new HashSet < > ( ) ; while ( ( temp != null && ! visited . contains ( temp ) ) ) { if ( temp . left != null && ! visited . contains ( temp . left ) ) temp = temp . left ; else if ( temp . right != null && ! visited . contains ( temp . right ) ) temp = temp . right ; else { System . out . printf ( " % d ▁ " , temp . data ) ; visited . add ( temp ) ; temp = head ; } } } public static void main ( String [ ] args ) { GFG gfg = new GFG ( ) ; gfg . root = new Node ( 8 ) ; gfg . root . left = new Node ( 3 ) ; gfg . root . right = new Node ( 10 ) ; gfg . root . left . left = new Node ( 1 ) ; gfg . root . left . right = new Node ( 6 ) ; gfg . root . left . right . left = new Node ( 4 ) ; gfg . root . left . right . right = new Node ( 7 ) ; gfg . root . right . right = new Node ( 14 ) ; gfg . root . right . right . left = new Node ( 13 ) ; gfg . postorder ( gfg . root ) ; } }