Problem 480723 · easy · Phase 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

        50
       /  \
     30    70
    /  \     \
  20   40    80
    \
     25

Input:  root = build_tree([50, 30, 70, 20, 40, None, 80, None, 25])
Output: [20, 80]

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…