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 in a BST with right subtree
What is the output of the inorder successor function for node 15 in the given BST?
DSA Go
type TreeNode struct { Val int Left *TreeNode Right *TreeNode } func inorderSuccessor(root, p *TreeNode) *TreeNode { if p.Right != nil { curr := p.Right for curr.Left != nil { curr = curr.Left } return curr } var succ *TreeNode curr := root for curr != nil { if p.Val < curr.Val { succ = curr curr = curr.Left } else if p.Val > curr.Val { curr = curr.Right } else { break } } return succ } // BST structure: // 20 // / \ // 10 30 // \ \ // 15 40 // Call: inorderSuccessor(root, node with Val=15)
Attempts:
2 left
💡 Hint
Remember, if the node has a right child, the successor is the leftmost node in the right subtree.
✗ Incorrect
Since node 15 has no right child, the successor is the lowest ancestor whose left child is also an ancestor of 15. That is node 20.
❓ Predict Output
intermediate2:00remaining
Inorder successor when node has no right subtree
What is the output of the inorder successor function for node 10 in the BST below?
DSA Go
type TreeNode struct { Val int Left *TreeNode Right *TreeNode } func inorderSuccessor(root, p *TreeNode) *TreeNode { var succ *TreeNode curr := root for curr != nil { if p.Val < curr.Val { succ = curr curr = curr.Left } else if p.Val > curr.Val { curr = curr.Right } else { break } } return succ } // BST structure: // 20 // / \ // 10 30 // / // 5 // Call: inorderSuccessor(root, node with Val=10)
Attempts:
2 left
💡 Hint
If the node has no right child, the successor is the lowest ancestor whose left child is also an ancestor of the node.
✗ Incorrect
Node 10 has no right subtree, so the successor is the ancestor node 20 which is the next bigger value.
🔧 Debug
advanced2:00remaining
Identify the error in inorder successor code
What error will this code produce when finding the inorder successor of node 25 in the BST below?
DSA Go
type TreeNode struct { Val int Left *TreeNode Right *TreeNode } func inorderSuccessor(root, p *TreeNode) *TreeNode { if p.Right != nil { curr := p.Right for curr.Right != nil { curr = curr.Right } return curr } var succ *TreeNode curr := root for curr != nil { if p.Val < curr.Val { succ = curr curr = curr.Left } else if p.Val > curr.Val { curr = curr.Right } else { break } } return succ } // BST structure: // 20 // / \ // 10 30 // / \ // 25 40 // Call: inorderSuccessor(root, node with Val=25)
Attempts:
2 left
💡 Hint
Check the traversal inside the right subtree for the successor.
✗ Incorrect
The code incorrectly traverses to the rightmost node in the right subtree instead of the leftmost. The successor of 25 is 30, and the code correctly returns it since 25 has no right subtree.
🧠 Conceptual
advanced1:00remaining
Inorder successor when node is the maximum element
What is the inorder successor of the maximum node in a BST?
Attempts:
2 left
💡 Hint
Think about the inorder traversal order and what comes after the largest value.
✗ Incorrect
The maximum node has no inorder successor because it is the last node in the inorder traversal.
🚀 Application
expert3:00remaining
Find inorder successor in BST without parent pointer
Given a BST and a node p without parent pointers, which approach correctly finds the inorder successor of p?
Attempts:
2 left
💡 Hint
Without parent pointers, you must start from the root to find the successor.
✗ Incorrect
Since p has no parent pointer, we start from root and move down. When moving left, we update successor. This finds the lowest ancestor whose left child is also ancestor of p.