A binary tree of terrain elevation samples is given in level-order form (a single line of space-separated tokens, null marking a missing child; every non-null node contributes exactly two following tokens for its children).
A node is called a peak if its value is strictly greater than the value of every child it has. A node with no children is automatically a peak (there is nothing to be greater than).
Count how many nodes in the tree are peaks.
Line 1: space-separated level-order tokens describing the binary tree (integers and the token null).
A single integer: the number of peak nodes. If the tree is empty, print 0.
Example 1
Input
5 3 8 9 1
Expected
3
Explanation
Root 5 has children 3 and 8; 5 < 8, so root is not a peak. Node 3 has one child 9 with 9 >= 3, so 3 is not a peak. Node 8 has one child 1 with 1 < 8, so 8 is a peak. Leaves 9 and 1 are peaks automatically. Total peaks: 8, 9, 1 = 3.
Example 2
Input
4 4 4
Expected
2
Explanation
Root's children equal its own value (4 >= 4), so the root is not a peak (needs strictly greater). Both leaf children are peaks. Total: 2.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →