Created
February 26, 2015 10:25
-
-
Save rohith2506/f3cce58d8527b806d0d3 to your computer and use it in GitHub Desktop.
Connect Nodes at same level -- (Amazon)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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