Count Nodes Equal to Average of Subtree

Given the root of a binary tree, return the number of nodes where the value of the node is equal to the average of the values in its subtree.

Note:

  • The average of n elements is the sum of the n elements divided by n and rounded down to the nearest integer.
  • A subtree of root is a tree consisting of root and all of its descendants.
Example 1
        4
       / \
      8   5
     / \   \
    0   1   6
Inputroot = [4,8,5,0,1,null,6]
Output5
The nodes with values 4, 5, 0, 1, and 6 each equal the rounded-down average of the values in their respective subtrees.
Example 2
        1
Inputroot = [1]
Output1
For the node with value 1, the average of its subtree is 1 / 1 = 1.

Constraints

  • The number of nodes in the tree is in the range [1, 1000].
  • 0 <= Node.val <= 1000

Asked at 5 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate