{"id":5441,"date":"2019-08-17T22:14:48","date_gmt":"2019-08-18T05:14:48","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5441"},"modified":"2019-08-17T22:17:48","modified_gmt":"2019-08-18T05:17:48","slug":"leetcode-1161-maximum-level-sum-of-a-binary-tree","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-1161-maximum-level-sum-of-a-binary-tree\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1161. Maximum Level Sum of a Binary Tree"},"content":{"rendered":"\n<p>Given the&nbsp;<code>root<\/code>&nbsp;of a binary tree, the level of its root is&nbsp;<code>1<\/code>,&nbsp;the level of its children is&nbsp;<code>2<\/code>,&nbsp;and so on.<\/p>\n\n\n\n<p>Return the&nbsp;<strong>smallest<\/strong>&nbsp;level&nbsp;<code>X<\/code>&nbsp;such that the sum of all the values of nodes at level&nbsp;<code>X<\/code>&nbsp;is&nbsp;<strong>maximal<\/strong>.<\/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\/2019\/05\/03\/capture.JPG\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input: <\/strong>[1,7,0,7,-8,null,null]\n<strong>Output: <\/strong>2\n<strong>Explanation: <\/strong>\nLevel 1 sum = 1.\nLevel 2 sum = 7 + 0 = 7.\nLevel 3 sum = 7 + -8 = -1.\nSo we return the level with the maximum sum which is level 2.\n<\/pre>\n\n\n\n<p><strong>Note:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>The number of nodes in the given tree is between&nbsp;<code>1<\/code>&nbsp;and&nbsp;<code>10^4<\/code>.<\/li><li><code>-10^5 &lt;= node.val &lt;= 10^5<\/code><\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: HashTable<\/strong><\/h2>\n\n\n\n<p>Use a hash table \/ array to store the level sum.<\/p>\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, 232 ms, 70.2 ms\nclass Solution {\npublic:\n  int maxLevelSum(TreeNode* root) {\n    vector<int> sums;\n    \n    function<void(TreeNode*, int)> preorder = [&](TreeNode* n, int d) {\n      if (!n) return;\n      while (sums.size() <= d) sums.push_back(0);\n      sums[d] += n->val;\n      preorder(n->left, d + 1);\n      preorder(n->right, d + 1);\n    };\n    preorder(root, 1);\n    \n    int max_sum = sums[1];\n    int ans = 1;\n    for (int i = 2; i < sums.size(); ++i)\n      if (sums[i] > max_sum) {\n        max_sum = sums[i];\n        ans = i;        \n      }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given the&nbsp;root&nbsp;of a binary tree, the level of its root is&nbsp;1,&nbsp;the level of its children is&nbsp;2,&nbsp;and so on. Return the&nbsp;smallest&nbsp;level&nbsp;X&nbsp;such that the sum of all&#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":[329,177,28],"class_list":["post-5441","post","type-post","status-publish","format-standard","hentry","category-tree","tag-level","tag-medium","tag-tree","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5441","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=5441"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5441\/revisions"}],"predecessor-version":[{"id":5445,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5441\/revisions\/5445"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5441"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5441"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5441"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}