0
0
DSA Typescriptprogramming~20 mins

BST Find Minimum Element in DSA Typescript - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
BST Minimum Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Find minimum element in a BST
What is the output of the following TypeScript code that finds the minimum element in a Binary Search Tree (BST)?
DSA Typescript
class Node {
  value: number;
  left: Node | null;
  right: Node | null;
  constructor(value: number) {
    this.value = value;
    this.left = null;
    this.right = null;
  }
}

function findMin(root: Node | null): number | null {
  if (!root) return null;
  let current = root;
  while (current.left !== null) {
    current = current.left;
  }
  return current.value;
}

const root = new Node(10);
root.left = new Node(5);
root.right = new Node(15);
root.left.left = new Node(2);
root.left.right = new Node(7);
root.right.right = new Node(20);

console.log(findMin(root));
A2
B5
C7
D10
Attempts:
2 left
💡 Hint
The minimum element in a BST is the leftmost node.
Predict Output
intermediate
2:00remaining
Minimum element when BST has only right children
What will the output be when finding the minimum element in a BST where all nodes only have right children?
DSA Typescript
class Node {
  value: number;
  left: Node | null;
  right: Node | null;
  constructor(value: number) {
    this.value = value;
    this.left = null;
    this.right = null;
  }
}

function findMin(root: Node | null): number | null {
  if (!root) return null;
  let current = root;
  while (current.left !== null) {
    current = current.left;
  }
  return current.value;
}

const root = new Node(3);
root.right = new Node(5);
root.right.right = new Node(7);
root.right.right.right = new Node(10);

console.log(findMin(root));
A7
B5
C3
D10
Attempts:
2 left
💡 Hint
If there are no left children, the root is the minimum.
🔧 Debug
advanced
2:00remaining
Identify the error in this BST minimum finder
What error will this TypeScript code produce when trying to find the minimum element in a BST?
DSA Typescript
class Node {
  value: number;
  left: Node | null;
  right: Node | null;
  constructor(value: number) {
    this.value = value;
    this.left = null;
    this.right = null;
  }
}

function findMin(root: Node | null): number | null {
  if (!root) return null;
  let current = root;
  while (current.left) {
    current = current.right;
  }
  return current.value;
}

const root = new Node(8);
root.left = new Node(3);
root.left.left = new Node(1);

console.log(findMin(root));
ATypeError: Cannot read property 'left' of null
B8
C1
D10
Attempts:
2 left
💡 Hint
Check the direction of traversal inside the while loop.
🧠 Conceptual
advanced
2:00remaining
Why does finding minimum in BST take O(h) time?
Why does finding the minimum element in a Binary Search Tree take time proportional to the height (h) of the tree?
ABecause the minimum element is always the root node, so it takes constant time.
BBecause the minimum is always found by traversing the left children from the root down to the leaf, which can be up to height h.
CBecause the minimum element is found by traversing all nodes in the tree, which takes O(n) time.
DBecause the minimum element is always the rightmost leaf, so traversal goes right h times.
Attempts:
2 left
💡 Hint
Think about how you move from root to the smallest value in a BST.
Predict Output
expert
2:00remaining
Output of findMin on a BST with duplicate values
What is the output of the following code that finds the minimum element in a BST containing duplicate values?
DSA Typescript
class Node {
  value: number;
  left: Node | null;
  right: Node | null;
  constructor(value: number) {
    this.value = value;
    this.left = null;
    this.right = null;
  }
}

function findMin(root: Node | null): number | null {
  if (!root) return null;
  let current = root;
  while (current.left !== null) {
    current = current.left;
  }
  return current.value;
}

const root = new Node(10);
root.left = new Node(10);
root.left.left = new Node(5);
root.left.left.left = new Node(5);
root.right = new Node(15);

console.log(findMin(root));
A15
B10
Cnull
D5
Attempts:
2 left
💡 Hint
Duplicates can appear on the left side; minimum is still the leftmost node.