Find the Minimum Value in a Binary Search Tree

Visualization

Algorithm Steps

  1. Start at the root node of the BST.
  2. If the tree is empty, return an error or null.
  3. Iteratively traverse to the left child while it exists.
  4. When a node with no left child is reached, that node contains the minimum value.
  5. Return the value of this node.

Find the Minimum Value in a Binary Search Tree - Code Examples Code

Python
Java
JavaScript
C
C++
C#
Kotlin
Swift
Go
Php
class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right

def findMin(root):
    if not root:
        return None
    while root.left:
        root = root.left
    return root.val

if __name__ == '__main__':
    # Construct BST:
    #         5
    #        / \
    #       3   7
    #      / \  / \
    #     2  4 6   8
    root = TreeNode(5, TreeNode(3, TreeNode(2), TreeNode(4)), TreeNode(7, TreeNode(6), TreeNode(8)))
    print(findMin(root))