0
0
DSA Goprogramming~10 mins

BST Inorder Successor in DSA Go - Execution Trace

Choose your learning style9 modes available
Concept Flow - BST Inorder Successor
Start at root node
Search for target node
Found target node?
NoReturn None
Yes
Does target have right child?
YesGo to right child
Go left as far as possible
Go up to parent until node is left child
Return found successor or None
Find the inorder successor by first locating the target node, then either going to its right subtree's leftmost node or moving up to find the nearest ancestor where target is in left subtree.
Execution Sample
DSA Go
func inorderSuccessor(root, p *TreeNode) *TreeNode {
    if p.Right != nil {
        p = p.Right
        for p.Left != nil {
            p = p.Left
        }
        return p
    }
    var succ *TreeNode
    for root != nil {
        if p.Val < root.Val {
            succ = root
            root = root.Left
        } else {
            root = root.Right
        }
    }
    return succ
}
This code finds the inorder successor of node p in a BST by checking right subtree or moving up ancestors.
Execution Table
StepOperationCurrent NodePointer ChangesVisual State
1Start at root to find target10None10 / \ 5 15 / \ \ 3 7 18
2Compare p.Val=7 with root.Val=1010Move root to left child10 / \ 5 15 / \ \ 3 7 18
3Compare p.Val=7 with root.Val=55Move root to right child10 / \ 5 15 / \ \ 3 7 18
4Compare p.Val=7 with root.Val=77Found target node10 / \ 5 15 / \ \ 3 7 18
5Check if p.Right exists7p.Right is nil7 has no right child
6Move root to 10 to find successor10succ = 10, root = root.Left (5)10 / \ 5 15 / \ \ 3 7 18
7p.Val=7 < root.Val=1010succ = 10, root = root.Left (5)succ=10
8p.Val=7 > root.Val=55root = root.Right (7)succ=10
9p.Val=7 == root.Val=77root = root.Right (nil)succ=10
10root is nil, stopnilReturn succ=10Successor is 10
💡 Traversal ends when root becomes nil; successor found or nil if none.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 6After Step 7After Step 8After Step 9Final
root.Val105771057nilnil
succ.Valnilnilnilnil1010101010
p.Val777777777
Key Moments - 3 Insights
Why do we check if the target node has a right child first?
Because if the target has a right child, the inorder successor is the leftmost node in that right subtree (see step 5 and 6 in execution_table).
Why do we move up to ancestors when the target has no right child?
Because the successor is the nearest ancestor for which the target node lies in its left subtree (see steps 6 to 10 where succ is updated).
Why does the traversal stop when root becomes nil?
Because we have exhausted the path to find a successor; if none found, successor is nil (step 10).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 5, what is true about node 7?
ANode 7 has a right child
BNode 7 has no right child
CNode 7 is the root
DNode 7 is the successor
💡 Hint
Check the 'Operation' and 'Pointer Changes' columns at step 5 in execution_table
At which step does the successor get assigned to node 10?
AStep 4
BStep 8
CStep 6
DStep 10
💡 Hint
Look at the 'Pointer Changes' column where succ is first set
If node 7 had a right child, how would the execution_table change?
AWe would go to the right child and find its leftmost node as successor
BWe would move up to ancestors instead
CThe successor would be nil immediately
DThe traversal would stop at root
💡 Hint
Refer to the concept_flow where right child presence leads to leftmost node search
Concept Snapshot
BST Inorder Successor:
- If node has right child, successor = leftmost node in right subtree
- Else, successor = nearest ancestor where node is in left subtree
- Search target node first, then apply above
- Returns nil if no successor exists
Full Transcript
To find the inorder successor in a BST, first locate the target node. If it has a right child, go to that right child and then keep going left until no more left children exist; that node is the successor. If no right child exists, move up the tree from the root, tracking the last node where you took a left turn; that node is the successor. If no such node exists, the successor is nil. This process ensures the next larger node in sorted order is found.