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 Go, what data structure is typically used to represent a BST node for inorder successor problems?
A struct with fields for the node's value, left child pointer, right child pointer, and optionally a parent pointer to help find ancestors.
Click to reveal answer
What is the inorder successor of the maximum node in a BST?
AThe root node
BThe node itself
CThe minimum node in the BST
DNil (no successor)
✗ Incorrect
The maximum node has no inorder successor because there is no node with a greater key.
If a node has a right child, where do you look to find its inorder successor?
ARight child's leftmost descendant
BRight child's right subtree
CLeft child's rightmost descendant
DParent 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 left child
BGo up to the lowest ancestor whose left child is also an ancestor of the node
CGo to the right child
DThere is no 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(1)
BO(log n)
CO(h)
DO(n)
✗ Incorrect
The time depends on the height h of the BST, which can be up to O(n) in worst case but generally O(log n).
Which of these is NOT needed to find the inorder successor of a node in a BST?
ARoot of the BST
BNode's parent pointer
CNode's right child
DNode's left child
✗ Incorrect
You do not need the root to find the inorder successor if you have the node and its parent pointers.
Explain how to find the inorder successor of a node in a BST when the node has a right child.
Think about the next bigger value in the tree.
You got /4 concepts.
Describe the steps to find the inorder successor when the node has no right child.
Consider the path from node to root.
You got /4 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 largest key smaller than the given node's key
B. The node with the smallest key greater 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 with a key greater than the current node's key.
Final Answer:
The node with the smallest key greater than the given node's key -> Option B
Quick Check:
Inorder successor = next larger node [OK]
Hint: Inorder successor is next bigger node in BST [OK]
Common Mistakes:
Confusing successor with predecessor
Thinking successor is always right child
Assuming successor is parent node
2. Which of the following Go function signatures correctly represents a function to find the inorder successor of a BST node?
easy
A. func inorderSuccessor(root *Node, p *Node) *Node
B. func inorderSuccessor(p *Node) *Node
C. func inorderSuccessor(root Node, p Node) Node
D. func inorderSuccessor(root *Node) int
Solution
Step 1: Analyze function parameters
To find the inorder successor, we need the BST root and the target node pointer, both as pointers to Node structs.
Step 2: Check return type
The function should return a pointer to the successor node, so return type is *Node.
Final Answer:
func inorderSuccessor(root *Node, p *Node) *Node -> Option A
Quick Check:
Function needs root and target node pointers, returns node pointer [OK]
Hint: Function needs root and target node pointers [OK]
Common Mistakes:
Passing nodes by value instead of pointer
Returning int instead of node pointer
Missing root parameter
3. Given the following BST and code snippet, what will be printed?
type Node struct {
Val int
Left *Node
Right *Node
}
func inorderSuccessor(root, p *Node) *Node {
var successor *Node
for root != nil {
if p.Val < root.Val {
successor = root
root = root.Left
} else {
root = root.Right
}
}
return successor
}
// BST structure:
// 20
// / \
// 10 30
// \
// 15
p := &Node{Val: 15}
succ := inorderSuccessor(root, p)
if succ != nil {
fmt.Println(succ.Val)
} else {
fmt.Println("nil")
}
medium
A. 20
B. 30
C. 15
D. nil
Solution
Step 1: Trace inorderSuccessor function for p=15
Start at root=20: 15 < 20, successor=20, move left to 10. At 10: 15 >= 10, move right to 15. At 15: 15 >= 15, move right (nil). Loop ends.
Step 2: Return successor node
Successor is 20, so succ.Val = 20 is printed.
Final Answer:
20 -> Option A
Quick Check:
Next bigger node after 15 is 20 [OK]
Hint: Successor is smallest node bigger than p [OK]
Common Mistakes:
Returning p itself as successor
Returning nil when right subtree is empty
Confusing successor with predecessor
4. The following Go code tries to find the inorder successor of a node in a BST but has a bug. What is the bug?
func inorderSuccessor(root, p *Node) *Node {
if p.Right != nil {
curr := p.Right
for curr.Left != nil {
curr = curr.Left
}
return curr
}
var successor *Node
for root != nil {
if p.Val < root.Val {
successor = root
root = root.Left
} else {
root = root.Right
}
}
return successor
}
medium
A. It does not handle the case when p has no right child
B. It incorrectly returns p.Right when p.Right is nil
C. It does not update successor when p.Val equals root.Val
D. It assumes root is always the BST root, which may not be true
Solution
Step 1: Understand the logic for successor search
The code tries to find successor by checking right subtree or walking from root to find successor.
Step 2: Identify the bug in root traversal
The code assumes root is the BST root, but if root is not the actual BST root or p is not reachable from root, the successor found may be incorrect.
Final Answer:
It assumes root is always the BST root, which may not be true -> Option D
Quick Check:
Root must be BST root to find correct successor [OK]
Hint: Ensure root is BST root, not arbitrary node [OK]
Common Mistakes:
Passing a root that is not the BST root
Not checking if p.Right is nil before access
Ignoring case when p has no right subtree
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 node p efficiently using this parent pointer? Choose the correct approach.
hard
A. Always move to the root and perform full inorder traversal to find successor
B. If p has left child, successor is rightmost node in left subtree; else move down to left child
C. If p has right child, successor is leftmost node in right subtree; else move up using parent until node is left child of its parent
D. Successor is always the parent node regardless of children
Solution
Step 1: Check if p has right child
If yes, successor is the leftmost node in p's right subtree.
Step 2: If no right child, move up using parent pointer
Keep moving up until current node is 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 move up using parent until node is left child of its parent -> Option C
Quick Check:
Use right subtree or climb parents to find successor [OK]
Hint: Use right subtree or climb parents via left child check [OK]
Common Mistakes:
Confusing left and right subtree roles
Always returning parent as successor
Performing full traversal instead of using parent pointer
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.