0
0
DSA Goprogramming~10 mins

Kth Smallest Element in BST in DSA Go - Execution Trace

Choose your learning style9 modes available
Concept Flow - Kth Smallest Element in BST
Start at root node
Traverse left subtree
Visit current node
Count visited nodes
Is count == k?
NoTraverse right subtree
Yes
Return current node value
Done
We go left as far as possible, visit nodes in order, count them, and stop when we reach the kth node.
Execution Sample
DSA Go
func kthSmallest(root *TreeNode, k int) int {
    count := 0
    var result int
    var inorder func(node *TreeNode)
    inorder = func(node *TreeNode) {
        if node == nil || count >= k {
            return
        }
        inorder(node.Left)
        count++
        if count == k {
            result = node.Val
            return
        }
        inorder(node.Right)
    }
    inorder(root)
    return result
}
This code finds the kth smallest value in a BST by doing an inorder traversal and counting nodes.
Execution Table
StepOperationCurrent NodeCountActionVisual State
1Start at root50Go left5 / \ 3 7 / \ 2 4 8
2Go left30Go left5 / \ 3 7 / \ 2 4 8
3Go left20Go left (nil)5 / \ 3 7 / \ 2 4 8
4Left nilnil0Return5 / \ 3 7 / \ 2 4 8
5Visit node21Count=1Visited: 2
6Check count21count != k(3), go rightVisited: 2
7Go rightnil1ReturnVisited: 2
8Return to parent32Count=2Visited: 2 -> 3
9Check count32count != k(3), go rightVisited: 2 -> 3
10Go right42Go left (nil)Visited: 2 -> 3
11Left nilnil2ReturnVisited: 2 -> 3
12Visit node43Count=3Visited: 2 -> 3 -> 4
13Check count43count == k(3), set result=4Visited: 2 -> 3 -> 4
14Stop traversal43ReturnResult found: 4
15Return resultroot3Return 4Final result: 4
💡 Traversal stops when count reaches k=3 at node with value 4.
Variable Tracker
VariableStartAfter Step 5After Step 8After Step 12Final
count01233
result00044
current node52344
Key Moments - 3 Insights
Why do we stop traversal immediately after finding the kth smallest element?
Because once count equals k (see step 13 in execution_table), we have found the kth smallest node, so continuing traversal is unnecessary and inefficient.
Why do we traverse left subtree first before visiting the current node?
Inorder traversal visits nodes in ascending order for BSTs. Left subtree nodes are smaller, so we visit them first (see steps 2-5).
What happens if the BST has fewer than k nodes?
The traversal finishes without count reaching k, so result remains default (0 here). The code returns 0 or an invalid value indicating kth element doesn't exist.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of count at step 8?
A2
B3
C1
D0
💡 Hint
Check the 'Count' column at step 8 in execution_table.
At which step does the traversal stop because count equals k?
AStep 12
BStep 14
CStep 13
DStep 15
💡 Hint
Look for the step where 'count == k' and result is set in execution_table.
If k was 2 instead of 3, what would be the result returned?
A2
B3
C4
D5
💡 Hint
Check variable_tracker for count and result values at count=2 in execution_table.
Concept Snapshot
Kth Smallest Element in BST:
- Use inorder traversal (left, node, right)
- Count nodes visited
- When count == k, return node value
- Stop traversal early for efficiency
- Works because inorder visits nodes in ascending order
Full Transcript
To find the kth smallest element in a binary search tree, we do an inorder traversal. This means we first go as far left as possible, then visit the current node, then go right. We keep a count of how many nodes we have visited. When the count reaches k, we have found the kth smallest element and return its value. The traversal stops immediately after finding this node to save time. If the tree has fewer than k nodes, the function returns 0 or a default value. This method works because inorder traversal visits nodes in ascending order in a BST.