Get the successor of a value in a BST rooted by given node. Returns int. — int get_successor(bst_node* node, int value) { if (node == NULL) return -1; bst_node* target = node; while (target->value != value) { if (value < target->value) { target = target->left; } else if (value > target->value) { target = target->right; } } // arrived at target node if (target->right != NULL) { // get min value of right subtree return get_min(target->right); } else { // get lowest ancestor that is a left child in the path to target value bst_node* successor = NULL; bst_node* ancestor = node; while (ancestor != NULL) { if (value < ancestor->value) { successor = ancestor; ancestor = ancestor->left; } else { ancestor = ancestor->right; } } return successor->value; } }
G
1.2K
Google Interview
This flashcard deck made by jwasham contains knowledge about google interview. For more details, please follow https://github.com/jwasham/google-interview-university