Connect nodes at same level using constant extra space | Iterative CPP program to connect nodes at same level using constant extra space ; Constructor that allocates a new node with the given data and NULL left and right pointers . ; 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 code ; Constructed binary tree is 10 / \ 8 2 / \ 3 90 ; Populates nextRight pointer in all nodes ; Let us check the values of nextRight pointers
#include <bits/stdc++.h> NEW_LINE #include <bits/stdc++.h> NEW_LINE using namespace std ; class node { public : int data ; node * left ; node * right ; node * nextRight ; node ( int data ) { this -> data = data ; this -> left = NULL ; this -> right = NULL ; this -> nextRight = NULL ; } } ; 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 ; } void connectRecur ( node * p ) { node * temp ; if ( ! p ) return ; p -> nextRight = NULL ; while ( p != NULL ) { node * q = p ; while ( q != NULL ) { if ( q -> left ) { if ( q -> right ) q -> left -> nextRight = q -> right ; else q -> left -> nextRight = getNextRight ( q ) ; } if ( q -> right ) q -> right -> nextRight = getNextRight ( q ) ; q = q -> nextRight ; } if ( p -> left ) p = p -> left ; else if ( p -> right ) p = p -> right ; else p = getNextRight ( p ) ; } } int main ( ) { node * root = new node ( 10 ) ; root -> left = new node ( 8 ) ; root -> right = new node ( 2 ) ; root -> left -> left = new node ( 3 ) ; root -> right -> right = new node ( 90 ) ; connectRecur ( root ) ; cout << " Following ▁ are ▁ populated ▁ nextRight ▁ pointers ▁ in ▁ the ▁ tree " " ▁ ( -1 ▁ is ▁ printed ▁ if ▁ there ▁ is ▁ no ▁ nextRight ) ▁ STRNEWLINE " ; cout << " nextRight ▁ of ▁ " << root -> data << " ▁ is ▁ " << ( root -> nextRight ? root -> nextRight -> data : -1 ) << endl ; cout << " nextRight ▁ of ▁ " << root -> left -> data << " ▁ is ▁ " << ( root -> left -> nextRight ? root -> left -> nextRight -> data : -1 ) << endl ; cout << " nextRight ▁ of ▁ " << root -> right -> data << " ▁ is ▁ " << ( root -> right -> nextRight ? root -> right -> nextRight -> data : -1 ) << endl ; cout << " nextRight ▁ of ▁ " << root -> left -> left -> data << " ▁ is ▁ " << ( root -> left -> left -> nextRight ? root -> left -> left -> nextRight -> data : -1 ) << endl ; cout << " nextRight ▁ of ▁ " << root -> right -> right -> data << " ▁ is ▁ " << ( root -> right -> right -> nextRight ? root -> right -> right -> nextRight -> data : -1 ) << endl ; return 0 ; }