Class Solution
java.lang.Object
g2501_2600.s2583_kth_largest_sum_in_a_binary_tree.Solution
2583 - Kth Largest Sum in a Binary Tree.<p>Medium</p>
<p>You are given the <code>root</code> of a binary tree and a positive integer <code>k</code>.</p>
<p>The <strong>level sum</strong> in the tree is the sum of the values of the nodes that are on the <strong>same</strong> level.</p>
<p>Return <em>the</em> <code>k<sup>th</sup></code> <em><strong>largest</strong> level sum in the tree (not necessarily distinct)</em>. If there are fewer than <code>k</code> levels in the tree, return <code>-1</code>.</p>
<p><strong>Note</strong> that two nodes are on the same level if they have the same distance from the root.</p>
<p><strong>Example 1:</strong></p>
<p><img src="https://assets.leetcode.com/uploads/2022/12/14/binaryytreeedrawio-2.png" alt="" /></p>
<p><strong>Input:</strong> root = [5,8,9,2,1,3,7,4,6], k = 2</p>
<p><strong>Output:</strong> 13</p>
<p><strong>Explanation:</strong> The level sums are the following:</p>
<ul>
<li>
<p>Level 1: 5. - Level 2: 8 + 9 = 17.</p>
</li>
<li>
<p>Level 3: 2 + 1 + 3 + 7 = 13.</p>
</li>
<li>
<p>Level 4: 4 + 6 = 10. The 2<sup>nd</sup> largest level sum is 13.</p>
</li>
</ul>
<p><strong>Example 2:</strong></p>
<p><img src="https://assets.leetcode.com/uploads/2022/12/14/treedrawio-3.png" alt="" /></p>
<p><strong>Input:</strong> root = [1,2,null,3], k = 1</p>
<p><strong>Output:</strong> 3</p>
<p><strong>Explanation:</strong> The largest level sum is 3.</p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is <code>n</code>.</li>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= Node.val <= 10<sup>6</sup></code></li>
<li><code>1 <= k <= n</code></li>
</ul>
-
Constructor Summary
Constructors -
Method Summary
-
Constructor Details
-
Solution
public Solution()
-
-
Method Details
-
kthLargestLevelSum
-