Problem 480723 · easy · Level 04 Non-Linear Data Structures

Coldest and Warmest Reading

binary search tree · iteration

A weather station keeps its temperature readings in a binary search tree with distinct values. Return [coldest, warmest], the smallest and the largest value in the tree. Return [] for an empty tree. Do not visit every node.

Examples

        46
       /  \
     28    63
    /  \     \
  15   37    84
    \
     24

Input:  root = build_tree([46, 28, 63, 15, 37, None, 84, None, 24])
Output: [15, 84]

Input:  root = build_tree([-3])
Output: [-3, -3]

Constraints

  • 0 <= number of nodes <= 3000
  • All values are distinct integers
  • O(h) time

Goals

  • Find the minimum by following left pointers only
  • Find the maximum by following right pointers only
Starting Python…