0
0
DSA Goprogramming~20 mins

BST Inorder Successor in DSA Go - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
BST Inorder Successor Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
ANode with Val=20
BNode with Val=30
CNode with Val=40
DNode 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.
Predict Output
intermediate
2: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)
ANode with Val=30
BNode with Val=5
CNode with Val=20
DNode 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.
🔧 Debug
advanced
2: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)
AReturns nil
BReturns node with Val=30
CReturns node with Val=25
DReturns node with Val=40
Attempts:
2 left
💡 Hint
Check the traversal inside the right subtree for the successor.
🧠 Conceptual
advanced
1:00remaining
Inorder successor when node is the maximum element
What is the inorder successor of the maximum node in a BST?
AThe minimum node in the BST
BThe root node
CThe parent of the maximum node
DNil (no successor)
Attempts:
2 left
💡 Hint
Think about the inorder traversal order and what comes after the largest value.
🚀 Application
expert
3: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?
ATraverse from root, keep track of potential successor when moving left, return last recorded successor
BTraverse from p to root using parent pointers to find successor
CReturn the right child of p if exists, else return p itself
DTraverse the entire tree inorder and return the node after p
Attempts:
2 left
💡 Hint
Without parent pointers, you must start from the root to find the successor.