Count Good Nodes in Binary Tree
MediumTrees
Decode it first. Clues, constraints, pattern and plan before any code.Call a node good if, on the path from the root down to it, no node has a larger value than it. The root is always good.
Given the root of a binary tree, return how many good nodes it has.
Trees in the examples are written in level order (top to bottom, left to right), with null for a missing child, the same way the tests build them.
[3, 1, 4, 3, null, 1, 5] → 4 (3, the lower 3, 4 and 5)
[3, 3, null, 4, 2] → 3 (an equal value still counts as good)
[1] → 1Constraints
1 <= number of nodes <= 10^5-10^4 <= Node.val <= 10^4