📖 Scenario: You are working with a Binary Search Tree (BST) that stores numbers. You want to find the smallest number in this tree. This is useful when you want to know the minimum value stored in a sorted structure.
🎯 Goal: Build a simple BST with given nodes, then write code to find and print the minimum element in the BST.
📋 What You'll Learn
Create a BST node class with value, left, and right properties
Manually build a BST by linking nodes with exact values
Write a function findMin that finds the minimum value in the BST
Print the minimum value found
💡 Why This Matters
🌍 Real World
Finding the minimum value in a BST is useful in databases, search engines, and any system that needs quick access to sorted data.
💼 Career
Understanding BST operations like finding minimum or maximum values is essential for software engineers working with data structures, algorithms, and performance optimization.
Progress0 / 4 steps
1
Create BST Node Class and Build Tree
Create a class called TreeNode with a constructor that takes a value of type number. It should have public properties value, left, and right initialized to null. Then create three nodes: root with value 10, leftChild with value 5, and rightChild with value 15. Link root.left to leftChild and root.right to rightChild.
DSA Typescript
Hint
Remember to create a class with value, left, and right properties. Then create nodes and link them properly.
2
Add a Function to Find Minimum Value
Create a function called findMin that takes a parameter node of type TreeNode | null and returns a number. Inside the function, use a while loop to move to the left child until node.left is null. Return the value of the last node reached.
DSA Typescript
Hint
Use a while loop to go left until no more left child exists, then return that node's value.
3
Call the Function to Find Minimum
Call the function findMin with the variable root and store the result in a variable called minValue.
DSA Typescript
Hint
Call findMin with root and save the result in minValue.
4
Print the Minimum Value
Print the value of the variable minValue using console.log.
DSA Typescript
Hint
Use console.log(minValue) to print the minimum value.
Practice
(1/5)
1. In a Binary Search Tree (BST), where is the minimum element always located?
easy
A. At any leaf node
B. At the rightmost node
C. At the root node
D. At the leftmost node
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 D
Quick Check:
Minimum element = leftmost node [OK]
Hint: Minimum is always the leftmost node in BST [OK]
Common Mistakes:
Confusing minimum with root node
Thinking minimum is at rightmost node
Assuming minimum can be any leaf
2. Which TypeScript code snippet correctly finds the minimum element in a BST node?
easy
A. while (node.right !== null) { node = node.right; } return node.value;
B. while (node.left !== null) { node = node.left; } return node.value;
C. if (node.value < min) min = node.value; return min;
D. return Math.min(node.value, node.left.value);
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 B
Quick Check:
Traverse left to find minimum [OK]
Hint: Traverse left child nodes until null to find minimum [OK]
Common Mistakes:
Traversing right child instead of left
Using Math.min without checking null nodes
Not looping until leftmost node
3. What will be the output of the following TypeScript code?
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));
medium
A. 7
B. 5
C. 2
D. 10
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 C
Quick Check:
Minimum value in BST = 2 [OK]
Hint: Minimum is leftmost node value, here 2 [OK]
Common Mistakes:
Returning root value instead of leftmost
Confusing left and right child nodes
Stopping traversal too early
4. Identify the bug in this TypeScript function to find the minimum element in a BST:
function findMin(node: Node | null): number {
if (node === null) return -1;
while (node.right !== null) {
node = node.right;
}
return node.value;
}
medium
A. Traverses right child instead of left child
B. Returns -1 for empty tree, should throw error
C. Does not check if node is null before accessing value
D. Returns node.value before loop ends
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 A
Quick Check:
Minimum found by left traversal, not right [OK]
Hint: Minimum is leftmost, so traverse left child nodes [OK]
Common Mistakes:
Using right child traversal for minimum
Ignoring null checks before access
Returning default -1 instead of handling empty tree
5. Given a BST where some nodes have duplicate values, how would you modify the findMin function to return the smallest unique value?
hard
A. Traverse entire tree and collect all values, then find minimum
B. Return the first leftmost node value without checking duplicates
C. Traverse leftmost nodes and skip duplicates by checking parent values
D. Use rightmost node value as minimum to avoid duplicates
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 A
Quick Check:
Full traversal needed for unique minimum [OK]
Hint: Traverse all nodes to find unique minimum value [OK]
Common Mistakes:
Assuming leftmost node is always unique minimum
Ignoring duplicates in traversal
Using rightmost node as minimum
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.