2265. Count Nodes Equal to Average of Subtree ⭐
Medium
Last updated
Medium
Last updated
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:
Example 2:
Constraints:
The number of nodes in the tree is in the range [1, 1000]
.
0 <= Node.val <= 1000
原本的程式碼又醜又笨,討論區這個解答我最喜歡:
Runtime: 0 ms, faster than 100.00%
Memory Usage: 4.3 MB, less than 92.31%