{"id":9973,"date":"2023-03-05T07:51:10","date_gmt":"2023-03-05T15:51:10","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9973"},"modified":"2023-03-05T07:52:24","modified_gmt":"2023-03-05T15:52:24","slug":"leetcode-2583-kth-largest-sum-in-a-binary-tree","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/tree\/leetcode-2583-kth-largest-sum-in-a-binary-tree\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2583. Kth Largest Sum in a Binary Tree"},"content":{"rendered":"\n<p>You are given the&nbsp;<code>root<\/code>&nbsp;of a binary tree and a positive integer&nbsp;<code>k<\/code>.<\/p>\n\n\n\n<p>The&nbsp;<strong>level sum<\/strong>&nbsp;in the tree is the sum of the values of the nodes that are on the&nbsp;<strong>same<\/strong>&nbsp;level.<\/p>\n\n\n\n<p>Return<em>&nbsp;the&nbsp;<\/em><code>k<sup>th<\/sup><\/code><em>&nbsp;<strong>largest<\/strong>&nbsp;level sum in the tree (not necessarily distinct)<\/em>. If there are fewer than&nbsp;<code>k<\/code>&nbsp;levels in the tree, return&nbsp;<code>-1<\/code>.<\/p>\n\n\n\n<p><strong>Note<\/strong>&nbsp;that two nodes are on the same level if they have the same distance from the root.<\/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\/2022\/12\/14\/binaryytreeedrawio-2.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> root = [5,8,9,2,1,3,7,4,6], k = 2\n<strong>Output:<\/strong> 13\n<strong>Explanation:<\/strong> The level sums are the following:\n- Level 1: 5.\n- Level 2: 8 + 9 = 17.\n- Level 3: 2 + 1 + 3 + 7 = 13.\n- Level 4: 4 + 6 = 10.\nThe 2<sup>nd<\/sup> largest level sum is 13.\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\/12\/14\/treedrawio-3.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> root = [1,2,null,3], k = 1\n<strong>Output:<\/strong> 3\n<strong>Explanation:<\/strong> The largest level sum is 3.\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&nbsp;<code>n<\/code>.<\/li><li><code>2 &lt;= n &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>1 &lt;= Node.val &lt;= 10<sup>6<\/sup><\/code><\/li><li><code>1 &lt;= k &lt;= n<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: DFS + Sorting<\/strong><\/h2>\n\n\n\n<p>Use DFS to traverse the tree and accumulate level sum. Once done, sort the level sums and find the k-th largest one.<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(n)<\/p>\n\n\n\n<p>Note: sum can be very large, use long long.<\/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  long long kthLargestLevelSum(TreeNode* root, int k) {\n    vector<long long> sums;\n    function<void(TreeNode*, int)> dfs = [&](TreeNode* root, int d) {\n      if (!root) return;\n      while (d >= sums.size()) sums.push_back(0);\n      sums[d] += root->val;\n      dfs(root->left, d + 1);\n      dfs(root->right, d + 1);\n    };\n    dfs(root, 0);\n    if (sums.size() < k) return -1;\n    sort(rbegin(sums), rend(sums));\n    return sums[k - 1];\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given the&nbsp;root&nbsp;of a binary tree and a positive integer&nbsp;k. The&nbsp;level sum&nbsp;in the tree is the sum of the values of the nodes that&#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":[33,177,15,28],"class_list":["post-9973","post","type-post","status-publish","format-standard","hentry","category-tree","tag-dfs","tag-medium","tag-sorting","tag-tree","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9973","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=9973"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9973\/revisions"}],"predecessor-version":[{"id":9975,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9973\/revisions\/9975"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9973"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9973"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9973"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}