Loading...
Loading...
You are given the root of a binary search tree, root, and two integers lo and hi. Return the count of nodes in root whose value falls within [lo, hi], inclusive on both ends.
Return 0 if no node's value falls in the range.
Your function receives root as a TreeNode. The node type is provided for you, with val, left, and right fields.
The examples below write the tree as its level-order traversal, where null marks a missing child of a listed node and trailing nulls are omitted. That is only how the input is displayed; the decoding is done for you.
root, n, is in the range [1,105].Node.val ≤109Node.val are unique.lo ≤ hi ≤109root is a valid binary search tree: every node's value is greater than every value in its left subtree and less than every value in its right subtree.Only 10, 15, and 7 fall within [7, 15]; 5, 3, and 18 fall outside, so the count is 3.
Only 10, 7, and 6 fall within [6, 10]; the rest fall outside, so the count is 3.
The only node has value 1, which is outside [2, 3], so the count is 0.