Minimum in a Binary Search Tree
EasyUpdated: Aug 2, 2025
Problem
Given a non-empty binary search tree (BST), write a function to find and return the minimum data value found in the tree.
Examples
Example 1
Input:
10
/ \
5 15
/ \
2 7
Output:
15
Explanation:
The maximum value in the tree is found in the rightmost node, which is 15.
Example 2
Input:
20
/
15
/
10
Output:
20
Explanation:
The maximum value is located in the rightmost node in the tree, which is 20.
Similar Problem
[Minimum in a Binary Tree](minimum-in-a-binary-tree) [Maximum in a Binary Tree](maximum-in-a-binary-tree) [Maximum in a Binary Search Tree](maximum-in-a-binary-search-tree)
Solution
Method 1 - Using BST Properties
The minimum value in a BST is always found in the leftmost node, as the BST property ensures that smaller values are consistently placed on the left subtree. Thus, starting at the root and following the left child pointers until no further left child exists will yield the minimum value.
Approach
- Start at the root of the BST.
- Traverse left until you find a node that does not have a left child.
- The value at this node is the minimum value in the BST.
Code
Java
class Solution {
public int findMaximum(TreeNode root) {
if (root == null) {
throw new IllegalArgumentException("Tree is empty");
}
// Iterative traversal to the right-most node
TreeNode current = root;
while (current.right != null) {
current = current.right;
}
return current.val; // Maximum value is in the right-most node
}
public static void main(String[] args) {
// Example: Create a BST and find the maximum value
Solution solution = new Solution();
TreeNode root = new TreeNode(40);
root.left = new TreeNode(30);
root.right = new TreeNode(50);
root.right.left = new TreeNode(45);
root.right.right = new TreeNode(60);
int maxValue = solution.findMaximum(root);
System.out.println("Maximum value: " + maxValue); // Output: 60
}
}
Python
class Solution:
def findMaximum(self, root):
if not root:
raise ValueError("Tree is empty")
# Iterative traversal to the right-most node
current = root
while current.right:
current = current.right
return current.val # Maximum value is in the right-most node
# Example: Create a BST and find the maximum value
if __name__ == "__main__":
solution = Solution()
# Example tree
root = TreeNode(40)
root.left = TreeNode(30)
root.right = TreeNode(50)
root.right.left = TreeNode(45)
root.right.right = TreeNode(60)
max_value = solution.findMaximum(root)
print("Maximum value:", max_value) # Output: 60
Complexity
- ⏰ Time complexity:
O(h), wherehis the height of the tree. For a balanced tree, the height is approximatelyO(log n), wherenis the number of nodes. In the worst case (completely unbalanced), the height isO(n). - 🧺 Space complexity:
O(1)