mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-01-11 02:58:13 +08:00
43 lines
1.8 KiB
HTML
43 lines
1.8 KiB
HTML
|
<p>我们从二叉树的根节点 <code>root</code> 开始进行深度优先搜索。</p>
|
|||
|
|
|||
|
<p>在遍历中的每个节点处,我们输出 <code>D</code> 条短划线(其中 <code>D</code> 是该节点的深度),然后输出该节点的值。(<em>如果节点的深度为 <code>D</code>,则其直接子节点的深度为 <code>D + 1</code>。根节点的深度为 <code>0</code>)。</em></p>
|
|||
|
|
|||
|
<p>如果节点只有一个子节点,那么保证该子节点为左子节点。</p>
|
|||
|
|
|||
|
<p>给出遍历输出 <code>S</code>,还原树并返回其根节点 <code>root</code>。</p>
|
|||
|
|
|||
|
<p> </p>
|
|||
|
|
|||
|
<p><strong>示例 1:</strong></p>
|
|||
|
|
|||
|
<p><strong><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2019/04/12/recover-a-tree-from-preorder-traversal.png" style="height: 200px; width: 320px;"></strong></p>
|
|||
|
|
|||
|
<pre><strong>输入:</strong>"1-2--3--4-5--6--7"
|
|||
|
<strong>输出:</strong>[1,2,5,3,4,6,7]
|
|||
|
</pre>
|
|||
|
|
|||
|
<p><strong>示例 2:</strong></p>
|
|||
|
|
|||
|
<p><strong><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2019/04/12/screen-shot-2019-04-10-at-114101-pm.png" style="height: 250px; width: 256px;"></strong></p>
|
|||
|
|
|||
|
<pre><strong>输入:</strong>"1-2--3---4-5--6---7"
|
|||
|
<strong>输出:</strong>[1,2,5,3,null,6,null,4,null,7]
|
|||
|
</pre>
|
|||
|
|
|||
|
<p><strong>示例 3:</strong></p>
|
|||
|
|
|||
|
<p><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2019/04/12/screen-shot-2019-04-10-at-114955-pm.png" style="height: 250px; width: 276px;"></p>
|
|||
|
|
|||
|
<pre><strong>输入:</strong>"1-401--349---90--88"
|
|||
|
<strong>输出:</strong>[1,401,null,349,88,90]
|
|||
|
</pre>
|
|||
|
|
|||
|
<p> </p>
|
|||
|
|
|||
|
<p><strong>提示:</strong></p>
|
|||
|
|
|||
|
<ul>
|
|||
|
<li>原始树中的节点数介于 <code>1</code> 和 <code>1000</code> 之间。</li>
|
|||
|
<li>每个节点的值介于 <code>1</code> 和 <code>10 ^ 9</code> 之间。</li>
|
|||
|
</ul>
|