AlgoFrames

Min and max

Standard · Write code · about 8 min

Given the root of a binary search tree, return a list [min, max] with the smallest and the largest key. Return an empty list for an empty tree.

Example 1

Input:

[[8,3,10,1,6,null,14,null,null,4,7,13]]

Output:

[1,14]

Left from 8 reaches 1. Right from 8 reaches 14.

Example 2

Input:

[[5]]

Output:

[5,5]

One node is both the minimum and the maximum.

Constraints

  • 0 ≤ number of nodes ≤ 1,000
  • Keys are distinct integers with -1,000 ≤ key ≤ 1,000
  • The tree is a valid BST, given in level order; null marks a missing child

Learn the idea first: BST: search, insert, min and max, a visual lesson.

More BST: search, insert, min and max problems