BST Find Minimum Element in DSA Typescript - Time & Space Complexity
Finding the smallest value in a Binary Search Tree (BST) helps us quickly access the minimum data. We want to understand how the time it takes grows as the tree gets bigger.
How does the search time change when the tree has more nodes?
Analyze the time complexity of the following code snippet.
function findMin(root: TreeNode | null): number | null {
if (root === null) return null;
let current = root;
while (current.left !== null) {
current = current.left;
}
return current.val;
}
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 from a node to its left child.
- How many times: At most once per level down the left side of the tree.
Each step moves one level down the left side of the tree until the smallest node is found.
| Input Size (n) | Approx. Operations (steps down left) |
|---|---|
| 10 | Up to 4-5 steps (depends on tree shape) |
| 100 | Up to 6-7 steps |
| 1000 | Up to 9-10 steps |
Pattern observation: The number of steps grows with the height of the tree, not the total nodes.
Time Complexity: O(h) where h is the tree height
This means the time depends on how tall the tree is, not how many nodes it has in total.
[X] Wrong: "Finding the minimum always takes time proportional to the total number of nodes."
[OK] Correct: The search only follows the left children, so it depends on the tree height, which can be much smaller than total nodes.
Understanding how tree height affects search time helps you explain why balanced trees are faster. This skill shows you can analyze and improve data structure performance.
What if the BST is perfectly balanced? 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 left until no more left child exists.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
Solution
Step 1: Check traversal direction for minimum
Minimum is found by moving left until no left child exists.Step 2: Verify code correctness
while (node.left !== null) { node = node.left; } return node.value; correctly loops left and returns node value; others are incorrect or incomplete.Final Answer:
while (node.left !== null) { node = node.left; } return node.value; -> Option BQuick Check:
Traverse left to find minimum [OK]
- Traversing right child instead of left
- Using Math.min without checking null nodes
- Not looping until leftmost node
class Node {
constructor(public value: number, public left: Node | null = null, public right: Node | null = null) {}
}
const root = new Node(10, new Node(5, new Node(2), new Node(7)), new Node(15));
function findMin(node: Node): number {
while (node.left !== null) {
node = node.left;
}
return node.value;
}
console.log(findMin(root));Solution
Step 1: Analyze BST structure
Root is 10, left child 5, left-left child 2, left-right child 7, right child 15.Step 2: Trace findMin function
Function moves left from 10 to 5, then to 2, which has no left child, so returns 2.Final Answer:
2 -> Option CQuick Check:
Minimum value in BST = 2 [OK]
- Returning root value instead of leftmost
- Confusing left and right child nodes
- Stopping traversal too early
function findMin(node: Node | null): number {
if (node === null) return -1;
while (node.right !== null) {
node = node.right;
}
return node.value;
}Solution
Step 1: Check traversal direction
Minimum element requires moving left, but code moves right.Step 2: Identify correct fix
Change 'node.right' to 'node.left' in the while loop to find minimum.Final Answer:
Traverses right child instead of left child -> Option AQuick Check:
Minimum found by left traversal, not right [OK]
- Using right child traversal for minimum
- Ignoring null checks before access
- Returning default -1 instead of handling empty tree
findMin function to return the smallest unique value?Solution
Step 1: Understand duplicates in BST
Duplicates can appear on left or right; leftmost node may not be unique minimum.Step 2: Modify approach to find unique minimum
Traverse entire tree to collect all values, then find the smallest unique value.Final Answer:
Traverse entire tree and collect all values, then find minimum -> Option AQuick Check:
Full traversal needed for unique minimum [OK]
- Assuming leftmost node is always unique minimum
- Ignoring duplicates in traversal
- Using rightmost node as minimum
Ready to prove it?
Pass the test and this pattern moves unknown
