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)/二叉树中第二小的节点 [second-minimum-node-in-a-binary-tree].html
2022-03-29 12:43:11 +08:00

36 lines
1.5 KiB
HTML
Raw Permalink 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>给定一个非空特殊的二叉树,每个节点都是正数,并且每个节点的子节点数量只能为&nbsp;<code>2</code>&nbsp;&nbsp;<code>0</code>。如果一个节点有两个子节点的话,那么该节点的值等于两个子节点中较小的一个。</p>
<p>更正式地说,即&nbsp;<code>root.val = min(root.left.val, root.right.val)</code> 总成立。</p>
<p>给出这样的一个二叉树,你需要输出所有节点中的&nbsp;<strong>第二小的值 </strong></p>
<p>如果第二小的值不存在的话,输出 -1 <strong></strong></p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/10/15/smbt1.jpg" style="height: 210px; width: 300px;" />
<pre>
<strong>输入:</strong>root = [2,2,5,null,null,5,7]
<strong>输出:</strong>5
<strong>解释:</strong>最小的值是 2 ,第二小的值是 5 。
</pre>
<p><strong>示例 2</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/10/15/smbt2.jpg" style="height: 113px; width: 200px;" />
<pre>
<strong>输入:</strong>root = [2,2,2]
<strong>输出:</strong>-1
<strong>解释:</strong>最小的值是 2, 但是不存在第二小的值。
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li>树中节点数目在范围 <code>[1, 25]</code></li>
<li><code>1 &lt;= Node.val &lt;= 2<sup>31</sup> - 1</code></li>
<li>对于树中每个节点 <code>root.val == min(root.left.val, root.right.val)</code></li>
</ul>