Complete the code to start searching for the minimum element from the root node.
function findMin(root) {
let current = [1];
while (current.left !== null) {
current = current.left;
}
return current.data;
}We start from the root node to find the minimum element in a BST.
Complete the code to move to the left child node while searching for the minimum.
function findMin(root) {
let current = root;
while (current.[1] !== null) {
current = current.left;
}
return current.data;
}We move left because the smallest value in a BST is always in the leftmost node.
Fix the error in the return statement to correctly return the minimum value.
function findMin(root) {
let current = root;
while (current.left !== null) {
current = current.left;
}
return current.[1];
}The node's value is stored in the 'data' property, so we return current.data.
Fill both blanks to create a function that returns null if the tree is empty and finds the minimum otherwise.
function findMin(root) {
if (root === [1]) {
return null;
}
let current = root;
while (current.[2] !== null) {
current = current.left;
}
return current.data;
}We check if root is null to handle empty trees, and traverse left to find the minimum.
Fill all three blanks to implement a recursive function that finds the minimum element in a BST.
function findMinRecursive(node) {
if (node === [1]) {
return null;
}
if (node.[2] === [3]) {
return node.data;
}
return findMinRecursive(node.left);
}The base case checks if node is null, then if left child is null, return current node's data.