BST Find Minimum Element in DSA Go - Time & Space Complexity
We want to understand how long it takes to find the smallest value in a Binary Search Tree (BST).
How does the time needed change as the tree grows bigger?
Analyze the time complexity of the following code snippet.
func findMin(root *Node) *Node {
current := root
for current.Left != nil {
current = current.Left
}
return current
}
This code finds the minimum element by moving left until no more left child exists.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Moving down the left child nodes in the tree.
- How many times: At most once per level of the tree, until the leftmost node is reached.
As the tree grows taller, the number of steps to find the minimum grows roughly with the height of the tree.
| Input Size (n) | Approx. Operations (steps down left) |
|---|---|
| 10 | Up to 3 (if tree is balanced) |
| 100 | Up to 6 (if tree is balanced) |
| 1000 | Up to 9 (if tree is balanced) |
Pattern observation: The steps grow slowly with input size if the tree is balanced, but can be as large as n if the tree is skewed.
Time Complexity: O(h)
This means the time depends on the height of the tree, which is the number of levels from root to the leftmost leaf.
[X] Wrong: "Finding the minimum always takes the same time regardless of tree shape."
[OK] Correct: If the tree is not balanced, the height can be as large as the number of nodes, making the search slower.
Understanding how tree shape affects search time helps you explain and improve data structure choices in real projects.
"What if the tree was balanced using a self-balancing method? How would the time complexity change?"
Practice
Solution
Step 1: Understand BST property
In a BST, left child nodes have smaller values than their parents.Step 2: Locate minimum element
The smallest value is found by going as far left as possible from the root.Final Answer:
At the leftmost node -> Option DQuick Check:
Minimum element = leftmost node [OK]
- Confusing minimum with root node
- Thinking minimum is at rightmost node
- Assuming minimum can be any leaf
type Node struct { Val int; Left, Right *Node }?Solution
Step 1: Check loop direction
To find minimum, traverse left child nodes until none left.Step 2: Verify return value
Return the value of the last leftmost node found.Final Answer:
func findMin(root *Node) int { for root.Left != nil { root = root.Left } return root.Val } -> Option BQuick Check:
Traverse left until nil, then return value [OK]
- Looping right child instead of left
- Returning root value without traversal
- Accessing right child value incorrectly
type Node struct {
Val int
Left, Right *Node
}
func findMin(root *Node) int {
for root.Left != nil {
root = root.Left
}
return root.Val
}
func main() {
root := &Node{Val: 10}
root.Left = &Node{Val: 5}
root.Right = &Node{Val: 15}
root.Left.Left = &Node{Val: 2}
root.Left.Right = &Node{Val: 7}
fmt.Println(findMin(root))
}
Solution
Step 1: Trace the BST structure
The leftmost node is root.Left.Left with value 2.Step 2: Apply findMin function
Function moves left until no more left child, returns 2.Final Answer:
2 -> Option AQuick Check:
Leftmost node value = 2 [OK]
- Returning root value instead of leftmost
- Confusing left and right child nodes
- Misreading tree structure
func findMin(root *Node) int {
if root == nil {
return 0
}
for root.Right != nil {
root = root.Right
}
return root.Val
}Solution
Step 1: Analyze traversal direction
Minimum element is found by going left, not right.Step 2: Identify correct loop condition
Loop should check root.Left != nil, not root.Right.Final Answer:
It should traverse left child nodes, not right -> Option AQuick Check:
Minimum = leftmost node, so traverse left [OK]
- Looping right child instead of left
- Returning 0 incorrectly for nil root
- Returning wrong node value
type Node struct {
Val int
Left, Right *Node
}
func findMin(root *Node) int {
// Your code here
}Solution
Step 1: Understand duplicate insertion rule
Duplicates go to right subtree, so left subtree has strictly smaller values.Step 2: Find minimum including duplicates
Minimum is still the leftmost node; duplicates do not affect left subtree.Step 3: Return smallest value found at leftmost node
Traverse left child nodes until nil, then return node value.Final Answer:
Traverse left child nodes until nil, then return node value -> Option CQuick Check:
Minimum = leftmost node including duplicates [OK]
- Skipping nodes equal to parent value incorrectly
- Traversing right subtree for minimum
- Ignoring duplicates in minimum search
Ready to prove it?
Pass the test and this pattern moves unknown
