|
| 1 | +# 2265. Count Nodes Equal to Average of Subtree |
| 2 | + |
| 3 | +- Difficulty: Medium. |
| 4 | +- Related Topics: Tree, Depth-First Search, Binary Tree. |
| 5 | +- Similar Questions: Maximum Average Subtree, Insufficient Nodes in Root to Leaf Paths, Count Nodes Equal to Sum of Descendants. |
| 6 | + |
| 7 | +## Problem |
| 8 | + |
| 9 | +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****. |
| 10 | + |
| 11 | +**Note:** |
| 12 | + |
| 13 | + |
| 14 | + |
| 15 | +- The **average** of `n` elements is the **sum** of the `n` elements divided by `n` and **rounded down** to the nearest integer. |
| 16 | + |
| 17 | +- A **subtree** of `root` is a tree consisting of `root` and all of its descendants. |
| 18 | + |
| 19 | + |
| 20 | + |
| 21 | +Example 1: |
| 22 | + |
| 23 | + |
| 24 | + |
| 25 | +``` |
| 26 | +Input: root = [4,8,5,0,1,null,6] |
| 27 | +Output: 5 |
| 28 | +Explanation: |
| 29 | +For the node with value 4: The average of its subtree is (4 + 8 + 5 + 0 + 1 + 6) / 6 = 24 / 6 = 4. |
| 30 | +For the node with value 5: The average of its subtree is (5 + 6) / 2 = 11 / 2 = 5. |
| 31 | +For the node with value 0: The average of its subtree is 0 / 1 = 0. |
| 32 | +For the node with value 1: The average of its subtree is 1 / 1 = 1. |
| 33 | +For the node with value 6: The average of its subtree is 6 / 1 = 6. |
| 34 | +``` |
| 35 | + |
| 36 | +Example 2: |
| 37 | + |
| 38 | + |
| 39 | + |
| 40 | +``` |
| 41 | +Input: root = [1] |
| 42 | +Output: 1 |
| 43 | +Explanation: For the node with value 1: The average of its subtree is 1 / 1 = 1. |
| 44 | +``` |
| 45 | + |
| 46 | + |
| 47 | +**Constraints:** |
| 48 | + |
| 49 | + |
| 50 | + |
| 51 | +- The number of nodes in the tree is in the range `[1, 1000]`. |
| 52 | + |
| 53 | +- `0 <= Node.val <= 1000` |
| 54 | + |
| 55 | + |
| 56 | + |
| 57 | +## Solution |
| 58 | + |
| 59 | +```javascript |
| 60 | +/** |
| 61 | + * Definition for a binary tree node. |
| 62 | + * function TreeNode(val, left, right) { |
| 63 | + * this.val = (val===undefined ? 0 : val) |
| 64 | + * this.left = (left===undefined ? null : left) |
| 65 | + * this.right = (right===undefined ? null : right) |
| 66 | + * } |
| 67 | + */ |
| 68 | +/** |
| 69 | + * @param {TreeNode} root |
| 70 | + * @return {number} |
| 71 | + */ |
| 72 | +var averageOfSubtree = function(root) { |
| 73 | + return helper(root).res; |
| 74 | +}; |
| 75 | + |
| 76 | +var helper = function(root) { |
| 77 | + if (!root) return { sum: 0, num: 0, res: 0 }; |
| 78 | + var left = helper(root.left); |
| 79 | + var right = helper(root.right); |
| 80 | + var sum = left.sum + right.sum + root.val; |
| 81 | + var num = left.num + right.num + 1; |
| 82 | + var res = left.res + right.res + (Math.floor(sum / num) === root.val ? 1 : 0); |
| 83 | + return { sum, num, res }; |
| 84 | +}; |
| 85 | +``` |
| 86 | + |
| 87 | +**Explain:** |
| 88 | + |
| 89 | +nope. |
| 90 | + |
| 91 | +**Complexity:** |
| 92 | + |
| 93 | +* Time complexity : O(n). |
| 94 | +* Space complexity : O(n). |
0 commit comments