1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-01-10 18:48:13 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
leetcode-problemset/leetcode-cn/problem (Chinese)/二叉树中的第 K 大层和 [kth-largest-sum-in-a-binary-tree].html
2023-03-24 20:17:23 +08:00

46 lines
1.5 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<p>给你一棵二叉树的根节点 <code>root</code> 和一个正整数 <code>k</code></p>
<p>树中的 <strong>层和</strong> 是指 <strong>同一层</strong> 上节点值的总和。</p>
<p>返回树中第 <code>k</code> 大的层和(不一定不同)。如果树少于 <code>k</code> 层,则返回 <code>-1</code></p>
<p><strong>注意</strong>,如果两个节点与根节点的距离相同,则认为它们在同一层。</p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2022/12/14/binaryytreeedrawio-2.png" style="width: 301px; height: 284px;" /></p>
<pre>
<strong>输入:</strong>root = [5,8,9,2,1,3,7,4,6], k = 2
<strong>输出:</strong>13
<strong>解释:</strong>树中每一层的层和分别是:
- Level 1: 5
- Level 2: 8 + 9 = 17
- Level 3: 2 + 1 + 3 + 7 = 13
- Level 4: 4 + 6 = 10
第 2 大的层和等于 13 。
</pre>
<p><strong>示例 2</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2022/12/14/treedrawio-3.png" style="width: 181px; height: 181px;" /></p>
<pre>
<strong>输入:</strong>root = [1,2,null,3], k = 1
<strong>输出:</strong>3
<strong>解释:</strong>最大的层和是 3 。
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li>树中的节点数为 <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>