...

/

Solution Review: Finding Minimum Value in a Binary Search Tree

Solution Review: Finding Minimum Value in a Binary Search Tree

Find a detailed analysis of the different ways to solve the “Finding Minimum Value in Binary Search Tree” challenge.

Solution #1: Iterative findMin() #

Press + to interact
main.cs
BST.cs
using System;
namespace chapter_6
{
class Solution
{
static public int findMin(Node rootNode)
{
// Traverse (in order) towards left till you reach leaf node,
//and then return leaf node's value
if (rootNode == null)
return -1;
while (rootNode.leftChild != null)
{
rootNode = rootNode.leftChild;
}
return rootNode.value;
}
static void Main(string[] args)
{
BinarySearchTree bsT = new BinarySearchTree(6);
bsT.insertBST(3);
bsT.insertBST(8);
bsT.insertBST(12);
bsT.insertBST(10);
bsT.insertBST(14);
Console.WriteLine(findMin(bsT.getRoot()));
return;
}
}
}

This solution first checks if the given root is null and returns -1 if it is (lines 10-11). Then, you move on to the left-subtree. Keep going to each node’s left child until the leftmost child is found (lines 13-16).

Time complexity

The time complexity of this solution is in ...