362. Count Nodes Equal to Average of Subtree

MediumTree

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.

Examples

Input: [4,8,5,0,1,null,6]

Output: 5

Explanation: Nodes 4, 5, 0, 1 and 6 each equal the floor average of their own subtree; node 8 (avg 3) does not. Answer is 5.

Constraints

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

Run checks all cases above. Submit evaluates all test cases.