{"id":9728,"date":"2022-05-10T00:12:15","date_gmt":"2022-05-10T07:12:15","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9728"},"modified":"2022-05-10T00:23:29","modified_gmt":"2022-05-10T07:23:29","slug":"leetcode-2265-count-nodes-equal-to-average-of-subtree","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-2265-count-nodes-equal-to-average-of-subtree\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2265. Count Nodes Equal to Average of Subtree"},"content":{"rendered":"\n<p>Given the&nbsp;<code>root<\/code>&nbsp;of a binary tree, return&nbsp;<em>the number of nodes where the value of the node is equal to the&nbsp;<strong>average<\/strong>&nbsp;of the values in its&nbsp;<strong>subtree<\/strong><\/em>.<\/p>\n\n\n\n<p><strong>Note:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>The&nbsp;<strong>average<\/strong>&nbsp;of&nbsp;<code>n<\/code>&nbsp;elements is the&nbsp;<strong>sum<\/strong>&nbsp;of the&nbsp;<code>n<\/code>&nbsp;elements divided by&nbsp;<code>n<\/code>&nbsp;and&nbsp;<strong>rounded down<\/strong>&nbsp;to the nearest integer.<\/li><li>A&nbsp;<strong>subtree<\/strong>&nbsp;of&nbsp;<code>root<\/code>&nbsp;is a tree consisting of&nbsp;<code>root<\/code>&nbsp;and all of its descendants.<\/li><\/ul>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2022\/03\/15\/image-20220315203925-1.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> root = [4,8,5,0,1,null,6]\n<strong>Output:<\/strong> 5\n<strong>Explanation:<\/strong> \nFor the node with value 4: The average of its subtree is (4 + 8 + 5 + 0 + 1 + 6) \/ 6 = 24 \/ 6 = 4.\nFor the node with value 5: The average of its subtree is (5 + 6) \/ 2 = 11 \/ 2 = 5.\nFor the node with value 0: The average of its subtree is 0 \/ 1 = 0.\nFor the node with value 1: The average of its subtree is 1 \/ 1 = 1.\nFor the node with value 6: The average of its subtree is 6 \/ 1 = 6.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2022\/03\/26\/image-20220326133920-1.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> root = [1]\n<strong>Output:<\/strong> 1\n<strong>Explanation:<\/strong> For the node with value 1: The average of its subtree is 1 \/ 1 = 1.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>The number of nodes in the tree is in the range&nbsp;<code>[1, 1000]<\/code>.<\/li><li><code>0 &lt;= Node.val &lt;= 1000<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Recursion<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(h)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int averageOfSubtree(TreeNode* root) {    \n    \/\/ Returns {sum(val), sum(node), ans}\n    function<tuple<int, int, int>(TreeNode*)> getSum \n      = [&](TreeNode* root) -> tuple<int, int, int> {\n      if (!root) return {0, 0, 0};\n      auto [lv, lc, la] = getSum(root->left);\n      auto [rv, rc, ra] = getSum(root->right);\n      int sum = lv + rv + root->val;\n      int count = lc + rc + 1;\n      int ans = la + ra + (root->val == sum \/ count);\n      return {sum, count, ans};\n    };    \n    return get<2>(getSum(root));\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given the&nbsp;root&nbsp;of a binary tree, return&nbsp;the number of nodes where the value of the node is equal to the&nbsp;average&nbsp;of the values in its&nbsp;subtree. Note: The&nbsp;average&nbsp;of&nbsp;n&nbsp;elements&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[45],"tags":[177,17,28],"class_list":["post-9728","post","type-post","status-publish","format-standard","hentry","category-tree","tag-medium","tag-recursion","tag-tree","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9728","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=9728"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9728\/revisions"}],"predecessor-version":[{"id":9730,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9728\/revisions\/9730"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9728"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9728"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9728"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}