Created
January 6, 2018 19:26
-
-
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.
This file contains 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
/* | |
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