Given a binary search tree with each node having an extra pointer next. Next should point to the node which is on the right side of the node and at the same level. Right most node points to NULL.
It's easier to understand and code: just use level-traversal, and connect at same time.
It's easier to understand and code: just use level-traversal, and connect at same time.
Read full article from Algorithms and Me: Binary Search Tree : Connect nodes at same levelvoid connectLevelRec(Node * node, int desired, Node **lastNode){if(node == NULL)return;//Node at the given levelif (desired ==1){//Update the next pointer of nodenode->next = *lastNode;//change the last node*lastNode = node;}// search for the node again at same levelconnectLevelRec(node->right, desired-1, lastNode);connectLevelRec(node->left, desired-1, lastNode);}