Given a Binary Search Tree (BST), find the minimum value stored in the tree. In a BST, the left child of a node contains a value less than the node itself, and the right child contains a value greater. You need to return the smallest value among all the nodes. If the tree is empty, return null
or an appropriate error/indicator.
Find the Minimum Value in a BST - Algorithm, Visualization, Examples
⬅ Previous TopicDelete a Node in a Binary Search Tree
Next Topic ⮕Find the Maximum Value in a Binary Search Tree
Next Topic ⮕Find the Maximum Value in a Binary Search Tree
Visualization Player
Solution
Algorithm Steps
Code
Python
Java
JavaScript
C
C++
C#
Go
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))