text: Populate Inorder Successor for all nodes | C # program to populate inorder traversal of all nodes ; An implementation that doesn 't use static variable A wrapper over populateNextRecur ; The first visited node will be the rightmost node next of the rightmost node will be NULL ; Set next of all descendants of p by traversing them in reverse Inorder ; First set the next pointer in right subtree ; Set the next as previously visited node in reverse Inorder ; Change the prev for subsequent node ; Finally , set the next pointer in right subtree
code: struct node { int data ; struct node * left ; struct node * right ; struct node * next ; } void populateNext ( struct node * root ) { struct node * next = NULL ; populateNextRecur ( root , & next ) ; } void populateNextRecur ( struct node * p , struct node * * next_ref ) { if ( p ) { populateNextRecur ( p -> right , next_ref ) ; p -> next = * next_ref ; * next_ref = p ; populateNextRecur ( p -> left , next_ref ) ; } }