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)/恢复二叉搜索树 [recover-binary-search-tree].html
2022-03-29 12:43:11 +08:00

32 lines
1.3 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> ,该树中的 <strong>恰好</strong> 两个节点的值被错误地交换。<em>请在不改变其结构的情况下,恢复这棵树&nbsp;</em></p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/10/28/recover1.jpg" style="width: 300px;" />
<pre>
<strong>输入:</strong>root = [1,3,null,null,2]
<strong>输出:</strong>[3,1,null,null,2]
<strong>解释:</strong>3 不能是 1 的左孩子,因为 3 &gt; 1 。交换 1 和 3 使二叉搜索树有效。
</pre>
<p><strong>示例 2</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/10/28/recover2.jpg" style="height: 208px; width: 400px;" />
<pre>
<strong>输入:</strong>root = [3,1,4,null,null,2]
<strong>输出:</strong>[2,1,4,null,null,3]
<strong>解释:</strong>2 不能在 3 的右子树中,因为 2 &lt; 3 。交换 2 和 3 使二叉搜索树有效。</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li>树上节点的数目在范围 <code>[2, 1000]</code></li>
<li><code>-2<sup>31</sup> &lt;= Node.val &lt;= 2<sup>31</sup> - 1</code></li>
</ul>
<p>&nbsp;</p>
<p><strong>进阶:</strong>使用 <code>O(n)</code> 空间复杂度的解法很容易实现。你能想出一个只使用&nbsp;<code>O(1)</code> 空间的解决方案吗?</p>