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)/二叉树展开为链表 [flatten-binary-tree-to-linked-list].html
2022-03-29 12:43:11 +08:00

43 lines
1.3 KiB
HTML
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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> ,请你将它展开为一个单链表:</p>
<ul>
<li>展开后的单链表应该同样使用 <code>TreeNode</code> ,其中 <code>right</code> 子指针指向链表中下一个结点,而左子指针始终为 <code>null</code></li>
<li>展开后的单链表应该与二叉树 <a href="https://baike.baidu.com/item/%E5%85%88%E5%BA%8F%E9%81%8D%E5%8E%86/6442839?fr=aladdin" target="_blank"><strong>先序遍历</strong></a> 顺序相同。</li>
</ul>
<p> </p>
<p><strong>示例 1</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/14/flaten.jpg" style="width: 500px; height: 226px;" />
<pre>
<strong>输入:</strong>root = [1,2,5,3,4,null,6]
<strong>输出:</strong>[1,null,2,null,3,null,4,null,5,null,6]
</pre>
<p><strong>示例 2</strong></p>
<pre>
<strong>输入:</strong>root = []
<strong>输出:</strong>[]
</pre>
<p><strong>示例 3</strong></p>
<pre>
<strong>输入:</strong>root = [0]
<strong>输出:</strong>[0]
</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li>树中结点数在范围 <code>[0, 2000]</code></li>
<li><code>-100 <= Node.val <= 100</code></li>
</ul>
<p> </p>
<p><strong>进阶:</strong>你可以使用原地算法(<code>O(1)</code> 额外空间)展开这棵树吗?</p>