0
0
DSA Javascriptprogramming~20 mins

BST Find Minimum Element in DSA Javascript - 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 code that finds the minimum element in a Binary Search Tree (BST)?
DSA Javascript
class Node {
  constructor(value) {
    this.value = value;
    this.left = null;
    this.right = null;
  }
}

function findMin(root) {
  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));
A5
B7
C2
D10
Attempts:
2 left
💡 Hint
The minimum element in a BST is the leftmost node.
Predict Output
intermediate
2:00remaining
Find minimum in BST with single node
What will the output be when finding the minimum element in a BST with only one node?
DSA Javascript
class Node {
  constructor(value) {
    this.value = value;
    this.left = null;
    this.right = null;
  }
}

function findMin(root) {
  let current = root;
  while (current.left !== null) {
    current = current.left;
  }
  return current.value;
}

const root = new Node(42);

console.log(findMin(root));
A42
Bnull
Cundefined
DError
Attempts:
2 left
💡 Hint
If there is only one node, that node is the minimum.
Predict Output
advanced
2:00remaining
Find minimum element in BST with null root
What error or output will this code produce when trying to find the minimum element in an empty BST (root is null)?
DSA Javascript
function findMin(root) {
  let current = root;
  while (current.left !== null) {
    current = current.left;
  }
  return current.value;
}

const root = null;

console.log(findMin(root));
Anull
BTypeError: Cannot read property 'left' of null
Cundefined
D0
Attempts:
2 left
💡 Hint
Check what happens when root is null and you try to access root.left.
🧠 Conceptual
advanced
2:00remaining
Why does findMin in BST only check left children?
Why does the function to find the minimum element in a BST only traverse left children and not right children?
ABecause right children are always larger, so checking them is unnecessary for minimum.
BBecause the minimum could be anywhere, but left is checked first by convention.
CBecause the function is incorrect and should check both sides.
DBecause in a BST, all left children are smaller than their parent nodes, so the minimum is found by going left.
Attempts:
2 left
💡 Hint
Think about the BST property for left and right children.
Predict Output
expert
2:00remaining
Find minimum element after BST modification
Given the BST below, what is the output after inserting a new node with value 1 and then finding the minimum element?
DSA Javascript
class Node {
  constructor(value) {
    this.value = value;
    this.left = null;
    this.right = null;
  }
}

function insert(root, value) {
  if (root === null) return new Node(value);
  if (value < root.value) {
    root.left = insert(root.left, value);
  } else {
    root.right = insert(root.right, value);
  }
  return root;
}

function findMin(root) {
  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);

insert(root, 1);

console.log(findMin(root));
A1
B2
C5
D10
Attempts:
2 left
💡 Hint
After inserting 1, it becomes the new leftmost node.