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 a path.
Click to reveal answer
beginner
In an inorder traversal, what is the order of nodes visited?
Inorder traversal visits nodes in ascending order: left subtree, current node, then right subtree.
Click to reveal answer
What is the inorder successor of a node with a right subtree?
AThe right child itself
BThe leftmost node in the right subtree
CThe parent node
DThe rightmost node in the left subtree
✗ Incorrect
The inorder successor is the smallest node greater than the current node, which is the leftmost node in the right subtree.
If a node has no right child, how do you find its inorder successor?
AThere is no inorder successor
BGo to the right child
CGo to the left child
DGo up to the lowest ancestor whose left child is also an ancestor of the node
✗ Incorrect
Without a right child, the successor is the lowest ancestor where the node lies in the left subtree.
What is the inorder successor of the maximum node in a BST?
ANo successor (null)
BThe root node
CThe minimum node
DThe parent node
✗ Incorrect
The maximum node has no node greater than it, so it has no inorder successor.
What traversal order does inorder successor relate to?
AInorder
BPreorder
CPostorder
DLevel order
✗ Incorrect
Inorder successor is the next node visited in inorder traversal.
What is the worst-case time complexity to find the inorder successor in a BST?
AO(n)
BO(log n)
CO(h)
DO(1)
✗ Incorrect
The time depends on the height h of the BST, which can be up to O(n) in unbalanced trees.
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 next bigger value in the sorted order of BST nodes.
You got /2 concepts.
Describe the relationship between inorder traversal and inorder successor in a BST.
Consider how nodes are ordered in inorder traversal.
You got /2 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 root node of the BST
D. The left 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 is the smallest node greater than it.
Final Answer:
The node with the smallest key greater than the given node's key -> Option A
Quick Check:
Inorder successor = smallest greater key [OK]
Hint: Inorder successor is next bigger node in sorted order [OK]
Common Mistakes:
Confusing successor with predecessor
Thinking successor is always right child
Assuming successor is parent node
2. Which of the following Java method signatures correctly represents a function to find the inorder successor in a BST node class?
easy
A. public void inorderSuccessor(Node p)
B. public Node inorderSuccessor(Node root, Node p)
C. public int inorderSuccessor(Node root)
D. public Node inorderSuccessor(int key)
Solution
Step 1: Identify required inputs and outputs
To find inorder successor, we need the BST root and the target node; output is the successor node.
Step 2: Match method signature
public Node inorderSuccessor(Node root, Node p) takes root and node, returns Node, matching requirements.
Final Answer:
public Node inorderSuccessor(Node root, Node p) -> Option B
Quick Check:
Input root and node, output node [OK]
Hint: Method needs root and target node, returns successor node [OK]
Common Mistakes:
Missing root parameter
Returning int instead of Node
Using void return type
3. Given the BST below and the node with value 15, what is the inorder successor?
20
/ \
10 30
\
15
medium
A. 20
B. 15
C. 30
D. 10
Solution
Step 1: Locate node 15 and check right subtree
Node 15 has no right child, so successor is ancestor with greater value.
Step 2: Find lowest ancestor greater than 15
Ancestor 20 is the next bigger node after 15.
Final Answer:
20 -> Option A
Quick Check:
Next bigger after 15 is 20 [OK]
Hint: If no right child, successor is nearest bigger ancestor [OK]
Common Mistakes:
Choosing 30 which is too big
Choosing 10 which is smaller
Choosing the node itself
4. Consider this buggy Java code snippet to find the inorder successor of a node in BST: