BST Find Maximum Element in DSA Go - Time & Space Complexity
We want to understand how long it takes to find the largest value in a Binary Search Tree (BST).
How does the time needed grow as the tree gets bigger?
Analyze the time complexity of the following code snippet.
func FindMax(root *Node) *Node {
if root == nil {
return nil
}
current := root
for current.Right != nil {
current = current.Right
}
return current
}
This code finds the maximum element by moving right until no more right child exists.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Moving from one node to its right child.
- How many times: At most once per level down the right side of the tree.
As the tree grows, the number of steps depends on the height of the tree, especially the rightmost path.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Up to 10 steps if tree is skewed right |
| 100 | Up to 100 steps in worst case |
| 1000 | Up to 1000 steps if very unbalanced |
Pattern observation: The time grows with the height of the tree, which can be as small as log n or as large as n.
Time Complexity: O(h)
This means the time depends on the tree's height, moving down the right side to find the max.
[X] Wrong: "Finding the max always takes the same time regardless of tree shape."
[OK] Correct: If the tree is unbalanced and tall, it takes longer because we must follow many right children.
Understanding how tree shape affects search time helps you explain and optimize tree operations clearly in interviews.
"What if the tree was balanced? How would the time complexity change when finding the maximum element?"
Practice
Solution
Step 1: Understand BST property
In a BST, all nodes to the right of a node have greater values.Step 2: Locate maximum element
The maximum element is found by moving to the rightmost node, as it has no greater right child.Final Answer:
At the rightmost node -> Option BQuick Check:
Maximum = rightmost node [OK]
- Confusing maximum with root node
- Thinking maximum is at leftmost node
- Assuming maximum can be any leaf
Solution
Step 1: Check loop condition for maximum
Maximum is found by moving right until no right child exists.Step 2: Verify code correctness
for current.Right != nil { current = current.Right } return current.Value correctly loops while current.Right is not nil, then returns current.Value.Final Answer:
for current.Right != nil { current = current.Right } return current.Value -> Option CQuick Check:
Loop right child until nil = max [OK]
- Looping left child instead of right
- Returning root value without traversal
- Loop condition incorrect or infinite
type Node struct {
Value int
Left *Node
Right *Node
}
func findMax(root *Node) int {
current := root
for current.Right != nil {
current = current.Right
}
return current.Value
}
root := &Node{Value: 10}
root.Left = &Node{Value: 5}
root.Right = &Node{Value: 15}
root.Right.Right = &Node{Value: 20}
fmt.Println(findMax(root))Solution
Step 1: Trace the BST structure
Root is 10, right child 15, right-right child 20 (rightmost node).Step 2: Follow findMax function
Function moves right until current.Right is nil, stopping at node with value 20.Final Answer:
20 -> Option AQuick Check:
Rightmost node value = 20 [OK]
- Returning root value instead of rightmost
- Stopping at first right child
- Confusing left and right pointers
func findMax(root *Node) int {
current := root
for current.Left != nil {
current = current.Left
}
return current.Value
}Solution
Step 1: Analyze traversal direction
The code loops while current.Left != nil, moving left, which finds minimum, not maximum.Step 2: Check for other issues
Function does not check if root is nil, which can cause runtime error.Final Answer:
It incorrectly traverses left children instead of right; does not handle nil root; returns minimum instead of maximum -> Option AQuick Check:
Left traversal + no nil check = bugs [OK]
- Using left instead of right pointer
- Ignoring nil root check
- Assuming code finds maximum correctly
Solution
Step 1: Understand duplicates in BST
In a BST allowing duplicates on right, right subtree values are >= current value.Step 2: Best traversal for maximum
Traverse right while current.Right != nil only. Value checks like in C (>) stop early on equal duplicates before larger values (e.g., 10->10->12).Final Answer:
Traverse right while current.Right != nil only -> Option DQuick Check:
Structural rightmost traversal = max even with duplicates [OK]
- Using strict > comparison, stopping early on equals
- Traversing left instead of right
- Returning root value without traversal
Ready to prove it?
Pass the test and this pattern moves unknown
