Challenge - 5 Problems
BST Inorder Predecessor Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Inorder Predecessor Function in BST
What is the output of the following C++ code that finds the inorder predecessor of a given node in a BST?
DSA C++
struct Node {
int data;
Node* left;
Node* right;
};
Node* inorderPredecessor(Node* root, Node* p) {
Node* predecessor = nullptr;
while (root) {
if (p->data <= root->data) {
root = root->left;
} else {
predecessor = root;
root = root->right;
}
}
return predecessor;
}
int main() {
Node n1{20, nullptr, nullptr};
Node n2{10, nullptr, nullptr};
Node n3{30, nullptr, nullptr};
Node n4{5, nullptr, nullptr};
Node n5{15, nullptr, nullptr};
n1.left = &n2; n1.right = &n3;
n2.left = &n4; n2.right = &n5;
Node* pred = inorderPredecessor(&n1, &n5);
if (pred) {
std::cout << pred->data << std::endl;
} else {
std::cout << "No predecessor" << std::endl;
}
return 0;
}Attempts:
2 left
💡 Hint
Think about the largest value smaller than the node 15 in the BST.
✗ Incorrect
The inorder predecessor of node 15 is the largest node smaller than 15. In this BST, 10 is the predecessor because it is less than 15 and the closest smaller value.
🧠 Conceptual
intermediate1:30remaining
Understanding Inorder Predecessor in BST
In a Binary Search Tree (BST), which of the following best describes the inorder predecessor of a node?
Attempts:
2 left
💡 Hint
Inorder traversal visits nodes in ascending order.
✗ Incorrect
Inorder predecessor is the node that comes immediately before the given node in inorder traversal, which means it has the largest value smaller than the given node.
🔧 Debug
advanced2:00remaining
Identify the Error in Inorder Predecessor Code
What error does the following C++ code produce when trying to find the inorder predecessor of a node in a BST?
DSA C++
Node* inorderPredecessor(Node* root, Node* p) {
if (!root) return nullptr;
if (p->left) {
Node* temp = p->left;
while (temp->right) temp = temp->right;
return temp;
}
Node* predecessor = nullptr;
while (root) {
if (p->data < root->data) {
root = root->left;
} else if (p->data > root->data) {
predecessor = root;
root = root->right;
}
}
return predecessor;
}
// Called with p = nullptrAttempts:
2 left
💡 Hint
Check what happens if p is nullptr.
✗ Incorrect
If p is nullptr, accessing p->left causes a segmentation fault because it dereferences a null pointer.
❓ Predict Output
advanced2:00remaining
Output of Inorder Predecessor for Root Node
What is the output of the following code when finding the inorder predecessor of the root node in a BST?
DSA C++
struct Node {
int data;
Node* left;
Node* right;
};
Node* inorderPredecessor(Node* root, Node* p) {
if (p->left) {
Node* temp = p->left;
while (temp->right) temp = temp->right;
return temp;
}
Node* predecessor = nullptr;
while (root) {
if (p->data <= root->data) {
root = root->left;
} else {
predecessor = root;
root = root->right;
}
}
return predecessor;
}
int main() {
Node n1{20, nullptr, nullptr};
Node n2{10, nullptr, nullptr};
Node n3{30, nullptr, nullptr};
n1.left = &n2; n1.right = &n3;
Node* pred = inorderPredecessor(&n1, &n1);
if (pred) {
std::cout << pred->data << std::endl;
} else {
std::cout << "No predecessor" << std::endl;
}
return 0;
}Attempts:
2 left
💡 Hint
Check if root has a left subtree and find its rightmost node.
✗ Incorrect
The root node 20 has a left child 10 with no right child, so the inorder predecessor is 10.
🚀 Application
expert2:30remaining
Number of Nodes with Inorder Predecessor in BST
Given a BST with nodes 5, 10, 15, 20, 25 inserted in that order, how many nodes have a valid inorder predecessor?
Attempts:
2 left
💡 Hint
The smallest node has no predecessor.
✗ Incorrect
Inorder predecessor exists for all nodes except the smallest one. Here, 5 is smallest, so 4 nodes have predecessors.