Skip to content

Instantly share code, notes, and snippets.

@rohith2506
Created February 26, 2015 10:25
Show Gist options
  • Save rohith2506/f3cce58d8527b806d0d3 to your computer and use it in GitHub Desktop.
Save rohith2506/f3cce58d8527b806d0d3 to your computer and use it in GitHub Desktop.
Connect Nodes at same level -- (Amazon)
// http://www.geeksforgeeks.org/connect-nodes-at-same-level/
void foo(t){
if(!t) return;
it(t -> left != NULL){
if(t->right != NULL){
t -> left -> next = t -> right;
}
else {
if(t->next->left != NULL){
t -> left -> next = t -> next -> left;
}
else if(t->next -> right != NULL){
t -> left -> next = t -> next -> right;
}
else
t -> left -> next = NULL;
}
}
if(t->right != NULL){
if(t->next != NULL){
t -> right -> next = t -> next -> left;
}
else
t -> right -> next = NULL;
}
foo(t->left);
foo(t->right);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment