879. Find Maximum in BST

EasyTreesBSTTraversal

Given the root of a non-empty binary search tree, return the maximum key — the value found by following right children to the end. Return -1 for an empty tree. The tree is given as a level-order BST array.

Input: A level-order BST array.

Output: Integer — the maximum key (or -1 if empty).

Examples

Example 1
Input: [8,3,10,1,6,null,14]
Output: 14
Explanation: Rightmost node holds the maximum.
Example 2
Input: [5]
Output: 5
Explanation: Single node.
Example 3
Input: [2,1,3]
Output: 3
Explanation: Rightmost is 3.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →