Notice Period

Record Setters on the Trail

medium

Trail markers on a mountain are arranged as a binary tree rooted at base camp, each marker stamped with an altitude. Hikers always walk from the root downward. A marker earns a record plaque if no marker on the path from base camp to it is stamped higher than it — ties still count as a record.

Given the root of a binary tree, return how many nodes are records: nodes whose value is greater than or equal to every value on the path from the root down to them. The root is always a record.

Tests pass a level-order array; implement the core in _count_good on real nodes.

Example 1
in values = [3, 1, 4, 3, null, 1, 5]
out 4
Records: the root 3, the 4, the 5, and the deep 3 (path 3 → 1 → 3 never exceeds 3).
Example 2
in values = [3, 3, null, 4, 2]
out 3
Root 3, the left 3 (tie counts), and the 4. The 2 is below a 3.
Example 3
in values = [1]
out 1
Constraints
  • · 1 <= number of nodes <= 10^5
  • · -10^4 <= node value <= 10^4
similar problem on LeetCode ↗
solution.pyloading python…
test results
Hit ▶ Run to test your code against the visible cases.