Challenge - 5 Problems
BST Inorder Successor Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateFind the inorder successor output
Given the BST and the node with value 15, what is the value of its inorder successor?
DSA C
struct Node {
int val;
struct Node* left;
struct Node* right;
};
// BST structure:
// 20
// / \
// 10 30
// \ \
// 15 40
// Node to find successor for: 15
// Inorder traversal: 10 -> 15 -> 20 -> 30 -> 40
// Inorder successor of 15 is the next node in inorder traversal.Attempts:
2 left
💡 Hint
Inorder successor is the next node in the inorder traversal sequence.
✗ Incorrect
Inorder traversal visits nodes in ascending order. After 15 comes 20, so 20 is the inorder successor.
❓ Predict Output
intermediateOutput of inorder successor function when node has right subtree
What will be the output of the inorder successor function for node with value 10 in the given BST?
DSA C
struct Node {
int val;
struct Node* left;
struct Node* right;
};
// BST structure:
// 10
// / \
// 5 15
// / \
// 12 20
// Node to find successor for: 10
// Inorder traversal: 5 -> 10 -> 12 -> 15 -> 20
// Inorder successor of 10 is the leftmost node in right subtree.Attempts:
2 left
💡 Hint
If node has right child, successor is leftmost node in right subtree.
✗ Incorrect
Node 10 has right subtree rooted at 15. The leftmost node there is 12, so successor is 12.
🔧 Debug
advancedIdentify the error in inorder successor code snippet
What error will this code produce when trying to find the inorder successor of a node in a BST?
DSA C
struct Node* inorderSuccessor(struct Node* root, struct Node* p) {
struct Node* successor = NULL;
while (root != NULL) {
if (p->val < root->val) {
successor = root;
root = root->left;
} else if (p->val > root->val) {
root = root->right;
} else {
if (root->right != NULL) {
root = root->right;
while (root->left != NULL) {
root = root->left;
}
return root;
}
break;
}
}
return successor;
}
// Error: What happens if p is NULL?Attempts:
2 left
💡 Hint
Check if p is NULL before accessing p->val.
✗ Incorrect
If p is NULL, accessing p->val causes a segmentation fault (crash).
🧠 Conceptual
advancedInorder successor when node has no right subtree
If a node in a BST has no right child, how do you find its inorder successor?
Attempts:
2 left
💡 Hint
Think about climbing up the tree to find a node greater than the current node.
✗ Incorrect
If no right subtree, successor is the lowest ancestor for which node is in left subtree.
🚀 Application
expertNumber of nodes with no inorder successor in BST
Given a BST with unique values, how many nodes have no inorder successor?
Attempts:
2 left
💡 Hint
Inorder successor is next higher value node; who has none?
✗ Incorrect
Only the node with the largest value has no inorder successor in a BST.
