Challenge - 5 Problems
BST Inorder Successor Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Find the inorder successor of a node in BST
What is the output of the following C++ code that finds the inorder successor of a node with value 15 in the BST?
DSA C++
#include <iostream> struct Node { int val; Node* left; Node* right; Node(int x) : val(x), left(nullptr), right(nullptr) {} }; Node* minValueNode(Node* node) { Node* current = node; while (current && current->left != nullptr) { current = current->left; } return current; } Node* inorderSuccessor(Node* root, Node* p) { if (p->right != nullptr) { return minValueNode(p->right); } Node* succ = nullptr; while (root != nullptr) { if (p->val < root->val) { succ = root; root = root->left; } else if (p->val > root->val) { root = root->right; } else { break; } } return succ; } int main() { Node* root = new Node(20); root->left = new Node(10); root->right = new Node(30); root->left->left = new Node(5); root->left->right = new Node(15); Node* p = root->left->right; // Node with value 15 Node* succ = inorderSuccessor(root, p); if (succ != nullptr) { std::cout << succ->val << std::endl; } else { std::cout << "null" << std::endl; } return 0; }
Attempts:
2 left
💡 Hint
Remember inorder successor is the smallest node greater than the given node.
✗ Incorrect
The inorder successor of 15 is 20 because 15 has no right child, so we look up to ancestors. 20 is the next bigger node after 15 in inorder traversal.
❓ Predict Output
intermediate2:00remaining
Inorder successor when node has right subtree
What will be printed by this C++ code when finding the inorder successor of node with value 10?
DSA C++
#include <iostream> struct Node { int val; Node* left; Node* right; Node(int x) : val(x), left(nullptr), right(nullptr) {} }; Node* minValueNode(Node* node) { Node* current = node; while (current && current->left != nullptr) { current = current->left; } return current; } Node* inorderSuccessor(Node* root, Node* p) { if (p->right != nullptr) { return minValueNode(p->right); } Node* succ = nullptr; while (root != nullptr) { if (p->val < root->val) { succ = root; root = root->left; } else if (p->val > root->val) { root = root->right; } else { break; } } return succ; } int main() { Node* root = new Node(20); root->left = new Node(10); root->right = new Node(30); root->left->left = new Node(5); root->left->right = new Node(15); Node* p = root->left; // Node with value 10 Node* succ = inorderSuccessor(root, p); if (succ != nullptr) { std::cout << succ->val << std::endl; } else { std::cout << "null" << std::endl; } return 0; }
Attempts:
2 left
💡 Hint
If the node has a right child, the successor is the leftmost node in that right subtree.
✗ Incorrect
Node 10 has a right child 15. The inorder successor is the smallest node in the right subtree, which is 15.
🔧 Debug
advanced2:00remaining
Identify the error in inorder successor code
What error will this code produce when trying to find the inorder successor of node with value 25?
DSA C++
#include <iostream> struct Node { int val; Node* left; Node* right; Node(int x) : val(x), left(nullptr), right(nullptr) {} }; Node* minValueNode(Node* node) { Node* current = node; while (current && current->left != nullptr) { current = current->left; } return current; } Node* inorderSuccessor(Node* root, Node* p) { if (p->right != nullptr) { return minValueNode(p->right); } Node* succ = nullptr; while (root != nullptr) { if (p->val < root->val) { succ = root; root = root->left; } else if (p->val > root->val) { root = root->right; } else { break; } } return succ; } int main() { Node* root = new Node(20); root->left = new Node(10); root->right = new Node(30); root->right->left = new Node(25); root->right->right = new Node(35); Node* p = root->right->left; // Node with value 25 Node* succ = inorderSuccessor(root, p); if (succ != nullptr) { std::cout << succ->val << std::endl; } else { std::cout << "null" << std::endl; } return 0; }
Attempts:
2 left
💡 Hint
Check if the node has a right child and what the successor should be.
✗ Incorrect
Node 25 has no right child. The successor is the lowest ancestor whose left child is also ancestor of 25, which is 30.
❓ Predict Output
advanced2:00remaining
Inorder successor for the maximum node in BST
What will this code print when finding the inorder successor of the node with value 35 (maximum node)?
DSA C++
#include <iostream> struct Node { int val; Node* left; Node* right; Node(int x) : val(x), left(nullptr), right(nullptr) {} }; Node* minValueNode(Node* node) { Node* current = node; while (current && current->left != nullptr) { current = current->left; } return current; } Node* inorderSuccessor(Node* root, Node* p) { if (p->right != nullptr) { return minValueNode(p->right); } Node* succ = nullptr; while (root != nullptr) { if (p->val < root->val) { succ = root; root = root->left; } else if (p->val > root->val) { root = root->right; } else { break; } } return succ; } int main() { Node* root = new Node(20); root->left = new Node(10); root->right = new Node(30); root->right->left = new Node(25); root->right->right = new Node(35); Node* p = root->right->right; // Node with value 35 Node* succ = inorderSuccessor(root, p); if (succ != nullptr) { std::cout << succ->val << std::endl; } else { std::cout << "null" << std::endl; } return 0; }
Attempts:
2 left
💡 Hint
The maximum node has no inorder successor in BST.
✗ Incorrect
Node 35 is the largest node and has no right child. No ancestor is greater than 35, so successor is null.
🧠 Conceptual
expert2:00remaining
Inorder successor concept in BST with parent pointers
Given a BST where each node has a parent pointer, which of the following is the correct approach to find the inorder successor of a node p?
Attempts:
2 left
💡 Hint
Think about inorder traversal order and how parent pointers help.
✗ Incorrect
Inorder successor is next node in inorder traversal. If right child exists, successor is leftmost node in right subtree. Otherwise, move up until you find a node that is left child of its parent; that parent is successor.