{"id":8646,"date":"2021-10-26T20:13:10","date_gmt":"2021-10-27T03:13:10","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8646"},"modified":"2021-10-26T20:19:43","modified_gmt":"2021-10-27T03:19:43","slug":"leetcode-2049-count-nodes-with-the-highest-score","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-2049-count-nodes-with-the-highest-score\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2049. Count Nodes With the Highest Score"},"content":{"rendered":"\n<p>There is a&nbsp;<strong>binary<\/strong>&nbsp;tree rooted at&nbsp;<code>0<\/code>&nbsp;consisting of&nbsp;<code>n<\/code>&nbsp;nodes. The nodes are labeled from&nbsp;<code>0<\/code>&nbsp;to&nbsp;<code>n - 1<\/code>. You are given a&nbsp;<strong>0-indexed<\/strong>&nbsp;integer array&nbsp;<code>parents<\/code>&nbsp;representing the tree, where&nbsp;<code>parents[i]<\/code>&nbsp;is the parent of node&nbsp;<code>i<\/code>. Since node&nbsp;<code>0<\/code>&nbsp;is the root,&nbsp;<code>parents[0] == -1<\/code>.<\/p>\n\n\n\n<p>Each node has a&nbsp;<strong>score<\/strong>. To find the score of a node, consider if the node and the edges connected to it were&nbsp;<strong>removed<\/strong>. The tree would become one or more&nbsp;<strong>non-empty<\/strong>&nbsp;subtrees. The&nbsp;<strong>size<\/strong>&nbsp;of a subtree is the number of the nodes in it. The&nbsp;<strong>score<\/strong>&nbsp;of the node is the&nbsp;<strong>product of the sizes<\/strong>&nbsp;of all those subtrees.<\/p>\n\n\n\n<p>Return&nbsp;<em>the&nbsp;<strong>number<\/strong>&nbsp;of nodes that have the&nbsp;<strong>highest score<\/strong><\/em>.<\/p>\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\/2021\/10\/03\/example-1.png\" alt=\"example-1\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> parents = [-1,2,0,2,0]\n<strong>Output:<\/strong> 3\n<strong>Explanation:<\/strong>\n- The score of node 0 is: 3 * 1 = 3\n- The score of node 1 is: 4 = 4\n- The score of node 2 is: 1 * 1 * 2 = 2\n- The score of node 3 is: 4 = 4\n- The score of node 4 is: 4 = 4\nThe highest score is 4, and three nodes (node 1, node 3, and node 4) have the highest score.\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\/2021\/10\/03\/example-2.png\" alt=\"example-2\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> parents = [-1,2,0]\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong>\n- The score of node 0 is: 2 = 2\n- The score of node 1 is: 2 = 2\n- The score of node 2 is: 1 * 1 = 1\nThe highest score is 2, and two nodes (node 0 and node 1) have the highest score.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>n == parents.length<\/code><\/li><li><code>2 &lt;= n &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>parents[0] == -1<\/code><\/li><li><code>0 &lt;= parents[i] &lt;= n - 1<\/code>&nbsp;for&nbsp;<code>i != 0<\/code><\/li><li><code>parents<\/code>&nbsp;represents a valid binary tree.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Recursion<\/strong><\/h2>\n\n\n\n<p>Write a function that returns the element of a subtree rooted at node.<\/p>\n\n\n\n<p>We can compute the score based on:<br>1. size of the subtree(s)<br>2. # of children<\/p>\n\n\n\n<p>Root is a special case whose score is max(c[0], 1) * max(c[1], 1).<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(n)<\/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 countHighestScoreNodes(vector<int>& parents) {\n    const int n = parents.size();\n    long high = 0;\n    int ans = 0;\n    vector<vector<int>> tree(n);\n    for (int i = 1; i < n; ++i)\n      tree[parents[i]].push_back(i);\n    function<int(int)> dfs = [&](int node) -> int {      \n      long c[2] = {0, 0};\n      for (int i = 0; i < tree[node].size(); ++i)\n        c[i] = dfs(tree[node][i]);\n      long score = 0;\n      if (node == 0) \/\/ case #1: root\n        score = max(c[0], 1l) * max(c[1], 1l);\n      else if (tree[node].size() == 0) \/\/ case #2: leaf\n        score = n - 1;\n      else if (tree[node].size() == 1) \/\/ case #3: one child\n        score = c[0] * (n - c[0] - 1);\n      else \/\/ case #4: two children\n        score = c[0] * c[1] * (n - c[0] - c[1] - 1);\n      if (score > high) {\n        high = score;\n        ans = 1;\n      } else if (score == high) {\n        ++ans;\n      }\n      return 1 + c[0] + c[1];\n    };\n    dfs(0);\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>There is a&nbsp;binary&nbsp;tree rooted at&nbsp;0&nbsp;consisting of&nbsp;n&nbsp;nodes. The nodes are labeled from&nbsp;0&nbsp;to&nbsp;n &#8211; 1. You are given a&nbsp;0-indexed&nbsp;integer array&nbsp;parents&nbsp;representing the tree, where&nbsp;parents[i]&nbsp;is the parent of node&nbsp;i.&#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":[77,177,17,28],"class_list":["post-8646","post","type-post","status-publish","format-standard","hentry","category-tree","tag-graph","tag-medium","tag-recursion","tag-tree","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8646","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=8646"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8646\/revisions"}],"predecessor-version":[{"id":8648,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8646\/revisions\/8648"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8646"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8646"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8646"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}