You are given the root of a Binary Search Tree (BST). Your task is to find and return the maximum value stored in the tree.
Return the largest value present in the BST.
Input: root = [5,3,8,1,4,null,9]
Output: 9
Explanation: The rightmost node holds the largest value, which is 9.
Input: root = [10,5]
Output: 10
Explanation: The root has no right subtree, so the root itself is the maximum.
Input: root = [42]
Output: 42
Explanation: A single-node BST has its maximum equal to the root value.
Approach, complexity and clean code in every language. Sign in to unlock the editorial.
Your submissions, results and runtime history live here. Sign in to start your streak.
50 30 70 20 40 60 80
80
40 20 60 10 30 50 70
70