Counting Maximal Value Roots in Binary Tree

Given a binary tree root, count and return the number of nodes where its value is greater than or equal to the values of all of its descendants.

For example, given

   6
  / \
 3   2
    / \
   6   4

Return 4 since all nodes except for 2 meet the criteria.

Example 1

Input

root = [6, [3, null, null], [2, [6, null, null], [4, null, null]]]

Output

4

TODO write preorder s postorder vs in order

Last updated