Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created January 6, 2018 19:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jianminchen/9f7c6b0ac4f5c44c9d20461f765bb9dd to your computer and use it in GitHub Desktop.
Save jianminchen/9f7c6b0ac4f5c44c9d20461f765bb9dd to your computer and use it in GitHub Desktop.
Find inorder successor - binary search tree - time complexity O(n), inorder traversal, find all nodes before given node. The solution is not optimal.
/*
Find binary search tree inorder successor
*/
bool found = false;
TreeNode result = NULL;
void findMe(TreeNode* root, TreeNode*givenNode){
if(!root)
return ;
findNode(root->left, givenNode); //9
if(root == givenNode){
found = true;
}
else if(found == true){ // precursor
found = false;
result = root;
return ;
}
findNode(root->right, givenNode);
}
TreeNode* findNode(TreeNode* root, TreeNode* givenNode){
findMe(root, givenNode);
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment