What is the inorder successor of a node in a Binary Search Tree (BST)?
The inorder successor of a node in a BST is the node with the smallest key greater than the given node's key. It is the next node visited in an inorder traversal.
Click to reveal answer
beginner
How do you find the inorder successor if the node has a right child?
If the node has a right child, the inorder successor is the leftmost node in the node's right subtree.
Click to reveal answer
intermediate
How do you find the inorder successor if the node has no right child?
If the node has no right child, the inorder successor is one of its ancestors. Specifically, it is the lowest ancestor of the node whose left child is also an ancestor of the node.
Click to reveal answer
intermediate
What is the time complexity of finding the inorder successor in a BST?
The time complexity is O(h), where h is the height of the BST, because you may need to traverse up or down the tree along the height.
Click to reveal answer
beginner
In Rust, what data structure is commonly used to represent a BST node?
A BST node in Rust is commonly represented using a struct with fields for the value and optional boxed pointers (Box or Rc) to left and right child nodes.
Click to reveal answer
What is the inorder successor of the maximum node in a BST?
AThe root node
BThere is no inorder successor
CThe minimum node in the BST
DThe right child of the maximum node
✗ Incorrect
The maximum node has no node with a greater key, so it has no inorder successor.
If a node has a right subtree, where do you look to find its inorder successor?
ARight child's leftmost node
BRight child's rightmost node
CParent node
DLeft child's rightmost node
✗ Incorrect
The inorder successor is the leftmost node in the right subtree.
If a node has no right child, how do you find its inorder successor?
AGo to the root node
BGo to the leftmost node
CGo up to the lowest ancestor whose left child is also an ancestor of the node
DThere is no inorder successor
✗ Incorrect
You move up the tree to find the lowest ancestor where the node is in the left subtree.
What is the worst-case time complexity to find the inorder successor in a BST?
AO(h)
BO(log n)
CO(1)
DO(n)
✗ Incorrect
The time depends on the height h of the BST, which can be up to O(n) in unbalanced trees.
In Rust, which smart pointer is commonly used to allow multiple ownership and mutability for BST nodes?
ABox<T>
BCell<T>
CArc<T>
DRc<RefCell<T>>
✗ Incorrect
Rc> allows multiple owners and interior mutability, useful for tree structures.
Explain how to find the inorder successor of a node in a BST when the node has a right child and when it does not.
Think about the inorder traversal order and where the next node lies.
You got /2 concepts.
Describe the time complexity of finding the inorder successor in a BST and what factors affect it.
Consider the longest path you might traverse up or down the tree.
You got /3 concepts.
Practice
(1/5)
1. What is the inorder successor of a node in a Binary Search Tree (BST)?
easy
A. The node with the smallest key greater than the given node's key
B. The node with the largest key smaller than the given node's key
C. The left child of the given node
D. The right child of the given node
Solution
Step 1: Understand inorder traversal order
Inorder traversal visits nodes in ascending order of their keys in a BST.
Step 2: Define inorder successor
The inorder successor of a node is the next node visited after it in inorder traversal, which means the smallest node with a key greater than the current node.
Final Answer:
The node with the smallest key greater than the given node's key -> Option A
Quick Check:
Inorder successor = next bigger node [OK]
Hint: Inorder successor is next bigger node in sorted order [OK]
Common Mistakes:
Confusing successor with predecessor
Thinking successor is always right child
Ignoring BST property in definition
2. Which Rust function signature correctly represents a method to find the inorder successor of a BST node?
easy
A. fn inorder_successor(root: &Option, p: &TreeNode) -> Option<&TreeNode>
B. fn inorder_successor(root: TreeNode, p: TreeNode) -> TreeNode
C. fn inorder_successor(root: &mut TreeNode, p: &mut TreeNode) -> TreeNode
D. fn inorder_successor(root: Option<TreeNode>, p: Option<TreeNode>) -> Option<TreeNode>
Solution
Step 1: Understand Rust ownership and references
BST nodes are usually wrapped in Option to allow null and heap allocation. Passing references avoids ownership transfer.
Step 2: Analyze function signature correctness
Option<&TreeNode> as return type allows returning a reference to a node or None. Input parameters as references to avoid moving ownership.
Final Answer:
fn inorder_successor(root: &Option, p: &TreeNode) -> Option<&TreeNode> -> Option A
Quick Check:
Use references and Option for safe BST traversal [OK]
Hint: Use references and Option types for BST nodes in Rust [OK]
Common Mistakes:
Passing nodes by value causing ownership errors
Returning owned nodes instead of references
Using mutable references unnecessarily
3. Given the following Rust code snippet for finding the inorder successor, what will be printed?
struct TreeNode {
val: i32,
left: Option,
right: Option,
}
fn inorder_successor(root: &Option, p_val: i32) -> Option<i32> {
let mut successor = None;
let mut current = root;
while let Some(node) = current {
if p_val < node.val {
successor = Some(node.val);
current = &node.left;
} else {
current = &node.right;
}
}
successor
}
fn main() {
let root = Some(Box::new(TreeNode {
val: 20,
left: Some(Box::new(TreeNode {
val: 10,
left: None,
right: Some(Box::new(TreeNode {
val: 15,
left: None,
right: None,
})),
})),
right: Some(Box::new(TreeNode {
val: 30,
left: None,
right: None,
})),
}));
let result = inorder_successor(&root, 15);
println!("{:?}", result);
}
medium
A. None
B. Some(20)
C. Some(15)
D. Some(30)
Solution
Step 1: Trace the search for successor of node with value 15
Start at root (20). Since 15 < 20, successor = 20, move left to node 10. 15 > 10, move right to node 15.
Step 2: Continue traversal and finalize successor
At node 15, 15 == 15, move right which is None. Loop ends. Last successor recorded is 20.
Final Answer:
Some(20) -> Option B
Quick Check:
Next bigger than 15 is 20 [OK]
Hint: Successor is smallest bigger node found going left [OK]
Common Mistakes:
Returning the node itself as successor
Confusing left and right subtree traversal
Not updating successor when moving left
4. The following Rust function aims to find the inorder successor of a node in a BST but has a bug. What is the bug?
fn inorder_successor(root: &Option, p_val: i32) -> Option<i32> {
let mut successor = None;
let mut current = root;
while let Some(node) = current {
if p_val <= node.val {
successor = Some(node.val);
current = &node.left;
} else {
current = &node.right;
}
}
successor
}
medium
A. Not handling None case for root
B. Function returns value instead of reference
C. Successor should be updated only when p_val > node.val
D. Using <= instead of < causes incorrect successor when p_val equals node.val
Solution
Step 1: Analyze condition p_val <= node.val
Using <= means successor updates even when p_val equals node.val, which incorrectly sets successor to the node itself.
Step 2: Correct condition for successor update
Successor should update only when p_val < node.val to find the next bigger node, not the node itself.
Final Answer:
Using <= instead of < causes incorrect successor when p_val equals node.val -> Option D
Quick Check:
Use < for successor update, not <= [OK]
Hint: Use strict less than (<) to avoid self as successor [OK]
Common Mistakes:
Confusing < and <= in comparisons
Ignoring edge case when p_val equals node.val
Not testing with equal values
5. You have a BST where each node has a parent pointer in addition to left and right children. How can you find the inorder successor of a given node p efficiently using this extra pointer?
hard
A. Successor is always the right child of p
B. Always go to the root and perform inorder traversal to find successor
C. If p has right child, successor is leftmost node in right subtree; else go up using parent until node is left child
D. Use a stack to store nodes and pop until successor is found
Solution
Step 1: Check if node has right subtree
If p has a right child, the successor is the leftmost node in that right subtree.
Step 2: If no right child, use parent pointer
Move up the tree using parent pointers until you find a node which is the left child of its parent; that parent is the successor.
Final Answer:
If p has right child, successor is leftmost node in right subtree; else go up using parent until node is left child -> Option C
Quick Check:
Use right subtree or climb parents to find successor [OK]
Hint: Use parent pointer to climb up when no right child [OK]
Common Mistakes:
Ignoring parent pointer advantage
Assuming successor is always right child
Performing full traversal unnecessarily
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.