Connect nodes at same level using constant extra space | Iterative c # program to connect nodes at same level using constant extra space ; A binary tree node ; This function returns the leftmost child of nodes at the same level as p . This function is used to getNExt right of p 's right child   If right child of is NULL then this can also be used for the   left child  ; Traverse nodes at p ' s ▁ level ▁ and ▁ find ▁ and ▁ return ▁ ▁ ▁ the ▁ first ▁ node ' s first child ; If all the nodes at p 's level are leaf nodes then return NULL  ; Sets nextRight of all nodes of a tree with root as p ; Set nextRight for root ; set nextRight of all levels one by one ; Connect all childrem nodes of p and children nodes of all other nodes at same level as p ; Set the nextRight pointer for p 's left child  ; If q has right child , then right child is nextRight of p and we also need to set nextRight of right child ; Set nextRight for other nodes in pre order fashion ; start from the first node of next level ; Driver program to test above functions ; Constructed binary tree is 10 / \ 8 2 / \ 3 90 ; Populates nextRight pointer in all nodes ; Let us check the values of nextRight pointers
using System ; public class Node { public int data ; public Node left , right , nextRight ; public Node ( int item ) { data = item ; left = right = nextRight = null ; } } public class BinaryTree { public Node root ; public virtual Node getNextRight ( Node p ) { Node temp = p . nextRight ; while ( temp != null ) { if ( temp . left != null ) { return temp . left ; } if ( temp . right != null ) { return temp . right ; } temp = temp . nextRight ; } return null ; } public virtual void connect ( Node p ) { Node temp = null ; if ( p == null ) { return ; } p . nextRight = null ; while ( p != null ) { Node q = p ; while ( q != null ) { if ( q . left != null ) { if ( q . right != null ) { q . left . nextRight = q . right ; } else { q . left . nextRight = getNextRight ( q ) ; } } if ( q . right != null ) { q . right . nextRight = getNextRight ( q ) ; } q = q . nextRight ; } if ( p . left != null ) { p = p . left ; } else if ( p . right != null ) { p = p . right ; } else { p = getNextRight ( p ) ; } } } public static void Main ( string [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 10 ) ; tree . root . left = new Node ( 8 ) ; tree . root . right = new Node ( 2 ) ; tree . root . left . left = new Node ( 3 ) ; tree . root . right . right = new Node ( 90 ) ; tree . connect ( tree . root ) ; int a = tree . root . nextRight != null ? tree . root . nextRight . data : - 1 ; int b = tree . root . left . nextRight != null ? tree . root . left . nextRight . data : - 1 ; int c = tree . root . right . nextRight != null ? tree . root . right . nextRight . data : - 1 ; int d = tree . root . left . left . nextRight != null ? tree . root . left . left . nextRight . data : - 1 ; int e = tree . root . right . right . nextRight != null ? tree . root . right . right . nextRight . data : - 1 ; Console . WriteLine ( " Following ▁ are ▁ populated ▁ nextRight ▁ pointers ▁ in ▁ " + " ▁ the ▁ tree ( -1 ▁ is ▁ printed ▁ if ▁ there ▁ is ▁ no ▁ nextRight ) " ) ; Console . WriteLine ( " nextRight ▁ of ▁ " + tree . root . data + " ▁ is ▁ " + a ) ; Console . WriteLine ( " nextRight ▁ of ▁ " + tree . root . left . data + " ▁ is ▁ " + b ) ; Console . WriteLine ( " nextRight ▁ of ▁ " + tree . root . right . data + " ▁ is ▁ " + c ) ; Console . WriteLine ( " nextRight ▁ of ▁ " + tree . root . left . left . data + " ▁ is ▁ " + d ) ; Console . WriteLine ( " nextRight ▁ of ▁ " + tree . root . right . right . data + " ▁ is ▁ " + e ) ; } }