Concept Flow - BST Find Minimum Element
Start at root
Check if left child exists?
No→Current node is minimum
Yes
Move to left child
Repeat check
Start at the root node and keep moving left until no left child exists; that node is the minimum.
func findMin(root *Node) *Node {
current := root
for current.Left != nil {
current = current.Left
}
return current
}| Step | Operation | Current Node | Left Child Exists? | Pointer Changes | Visual State |
|---|---|---|---|---|---|
| 1 | Start at root | 20 | Yes | current = root (20) | 20 / \ 10 30 |
| 2 | Check left child | 20 | Yes | Move current to left child (10) | 20 / \ 10 30 |
| 3 | Check left child | 10 | Yes | Move current to left child (5) | 20 / \ 10 30 / 5 |
| 4 | Check left child | 5 | No | Stop, current is minimum | 20 / \ 10 30 / 5 |
| 5 | Return current | 5 | No | Return node with value 5 | Minimum element found: 5 |
| Variable | Start | After Step 2 | After Step 3 | After Step 4 | Final |
|---|---|---|---|---|---|
| current | 20 (root) | 10 | 5 | 5 | 5 |
BST Find Minimum Element: - Start at root node - While left child exists, move left - When no left child, current node is minimum - Return current node - Works because BST left subtree has smaller values
type Node struct { Val int; Left, Right *Node }?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))
}
func findMin(root *Node) int {
if root == nil {
return 0
}
for root.Right != nil {
root = root.Right
}
return root.Val
}type Node struct {
Val int
Left, Right *Node
}
func findMin(root *Node) int {
// Your code here
}Pass the test and this pattern moves unknown