Delete a given value from a BST rooted at given node. Returns a pointer to node. — bst_node* delete_value(bst_node* node, int value) { if (node == NULL) return node; if (value < node->value) { node->left = delete_value(node->left, value); } else if (value > node->value) { node->right = delete_value(node->right, value); } else { // found value if (node->left == NULL && node->right == NULL) { free(node); node = NULL; } else if (node->left == NULL) { bst_node* temp = node; node = node->right; free(temp); } else if (node->right == NULL) { bst_node* temp = node; node = node->left; free(temp); } else { // 2 children - get min node of right subtree int right_min = get_min(node->right); node->value = right_min; node->right = delete_value(node->right, right_min); } } return node; }
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