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)/将二叉搜索树转化为排序的双向链表 [er-cha-sou-suo-shu-yu-shuang-xiang-lian-biao-lcof].html
2023-12-09 18:53:53 +08:00

55 lines
2.0 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>将一个 <strong>二叉搜索树</strong> 就地转化为一个 <strong>已排序的双向循环链表</strong></p>
<p>对于双向循环列表,你可以将左右孩子指针作为双向循环链表的前驱和后继指针,第一个节点的前驱是最后一个节点,最后一个节点的后继是第一个节点。</p>
<p>特别地,我们希望可以 <strong>就地</strong> 完成转换操作。当转化完成以后,树中节点的左指针需要指向前驱,树中节点的右指针需要指向后继。还需要返回链表中最小元素的指针。</p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<pre>
<strong>输入:</strong>root = [4,2,5,1,3]
<img src="https://assets.leetcode.com/uploads/2018/10/12/bstdllreturndll.png" />
<strong>输出:</strong>[1,2,3,4,5]
<strong>解释:</strong>下图显示了转化后的二叉搜索树,实线表示后继关系,虚线表示前驱关系。
<img src="https://assets.leetcode.com/uploads/2018/10/12/bstdllreturnbst.png" />
</pre>
<p><strong>示例 2</strong></p>
<pre>
<strong>输入:</strong>root = [2,1,3]
<strong>输出:</strong>[1,2,3]
</pre>
<p><strong>示例 3</strong></p>
<pre>
<strong>输入:</strong>root = []
<strong>输出:</strong>[]
<strong>解释:</strong>输入是空树,所以输出也是空链表。
</pre>
<p><strong>示例 4</strong></p>
<pre>
<strong>输入:</strong>root = [1]
<strong>输出:</strong>[1]
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>-1000 &lt;= Node.val &lt;= 1000</code></li>
<li><code>Node.left.val &lt; Node.val &lt; Node.right.val</code></li>
<li><code>Node.val</code> 的所有值都是独一无二的</li>
<li><code>0 &lt;= Number of Nodes &lt;= 2000</code></li>
</ul>
<p>注意:本题与主站 426 题相同:<a href="https://leetcode-cn.com/problems/convert-binary-search-tree-to-sorted-doubly-linked-list/" rel="noopener noreferrer" target="_blank">https://leetcode-cn.com/problems/convert-binary-search-tree-to-sorted-doubly-linked-list/</a></p>