CJCoding With Joseph
15free / daySign inCreate account

Find the Smallest Value in a BST

Complete findMin() so it returns the smallest value stored in the tree.

Reminder: what a binary search tree (BST) is. A BST is built from nodes. Each node holds a value and up to two children, left and right, and the node at the very top is the root. The BST rule: every value in a node's left subtree is smaller than the node, and every value in its right subtree is larger. This tree never stores the same value twice.

        50
       /  \
     30    70
    /  \     \
  20    40    80

Because smaller values always go left, the smallest value is the node you reach by stepping left from the root until there is no left child. You never need to look at a right child.

Requirements: 1. Return -1 when the tree is empty. 2. Otherwise start at root and walk down the left links with a loop, then return the value of the node you stop on. 3. Do not change the tree.

The Node class is provided - do not rename it and do not add public to it, because a Java source file may contain only one public class.

Do NOT write a main method.

Expected Output:

20
5

= newline. The output ends with a newline — print one after the last line.

Hints4 available
Topics:
Data Structures & AlgorithmsBinary Search Trees
1
Tab to indent (Esc then Tab to move on) · Ctrl+Enter to run · Shift+Alt+F to format · Ctrl+Space to expand shortcuts (sysout, psvm, fori)

Your Output

Run your code to see the output here...

Test Cases

Run your code to see test case results.