Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

BST Maximum

BSTeasy

Implement `bstMax(root)` returning the largest value in a non-empty BST.

Sample tests

Input: bstMax({"val":5,"left":{"val":3,"left":null,"right":null},"right":{"val":8,"left":null,"right":null}})
Output: 8
Input: bstMax({"val":5,"left":null,"right":null})
Output: 5

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • Following left links would find the minimum instead.

Learning resources

  • Wikipedia: Binary search tree
Approach & explanation (try first)

The largest value in a BST is the rightmost node, found in O(h).

Loading...
⌘/Ctrl + Enter

Run your code to see results.