Given the BST below, what is the inorder successor of the node with value 15?
BST structure:
20
/ \
10 30
\ \
15 40struct TreeNode {
val: i32,
left: Option<Box<TreeNode>>,
right: Option<Box<TreeNode>>,
}
// Function to find inorder successor of a given node value
fn inorder_successor(root: &Option<Box<TreeNode>>, p_val: i32) -> Option<i32> {
let mut successor = None;
let mut current = root.as_ref();
while let Some(node) = current {
if p_val < node.val {
successor = Some(node.val);
current = node.left.as_ref();
} else {
current = node.right.as_ref();
}
}
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: Some(Box::new(TreeNode {
val: 40,
left: None,
right: None,
})),
})),
}));
let result = inorder_successor(&root, 15);
println!("{:?}", result);
}Inorder successor is the smallest node greater than the given node.
The inorder successor of 15 is 20 because 20 is the next larger node in the BST after 15.
What is the output of the inorder_successor function when searching for the successor of node 40 in the BST below?
25
/ \
15 50
/ \ \
10 22 70struct TreeNode {
val: i32,
left: Option<Box<TreeNode>>,
right: Option<Box<TreeNode>>,
}
fn inorder_successor(root: &Option<Box<TreeNode>>, p_val: i32) -> Option<i32> {
let mut successor = None;
let mut current = root.as_ref();
while let Some(node) = current {
if p_val < node.val {
successor = Some(node.val);
current = node.left.as_ref();
} else {
current = node.right.as_ref();
}
}
successor
}
fn main() {
let root = Some(Box::new(TreeNode {
val: 25,
left: Some(Box::new(TreeNode {
val: 15,
left: Some(Box::new(TreeNode {
val: 10,
left: None,
right: None,
})),
right: Some(Box::new(TreeNode {
val: 22,
left: None,
right: None,
})),
})),
right: Some(Box::new(TreeNode {
val: 50,
left: None,
right: Some(Box::new(TreeNode {
val: 70,
left: None,
right: None,
})),
})),
}));
let result = inorder_successor(&root, 40);
println!("{:?}", result);
}Find the smallest node greater than 40.
40 is less than 50, so successor is updated to 50. The traversal moves left but no smaller successor found. So successor is 50.
What error will this Rust code produce when trying to find the inorder successor?
fn inorder_successor(root: &Option<Box<TreeNode>>, p_val: i32) -> Option<i32> {
let mut successor = None;
let mut current = root.as_ref();
while let Some(node) = current {
if p_val > node.val {
successor = Some(node.val);
current = node.left.as_ref();
} else {
current = node.right.as_ref();
}
}
successor
}Check the comparison direction and subtree traversal.
The condition is reversed. It updates successor when p_val > node.val and moves left, which is incorrect logic for inorder successor.
In a BST, if a node has a right child, what is the inorder successor of that node?
Think about inorder traversal order.
The inorder successor of a node with a right subtree is the smallest node in that right subtree, which is the leftmost node there.
Given a BST and a target node value, how can you find the inorder successor without using parent pointers?
Choose the correct approach.
Think about how inorder successor is found in different cases.
Finding inorder successor without parent pointers requires combining approaches: if right subtree exists, go there and find leftmost node; else traverse from root keeping track of successor; or do inorder traversal with stack.
