1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-01-11 02:58:13 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
leetcode-problemset/leetcode-cn/problem (Chinese)/根据前序和后序遍历构造二叉树 [construct-binary-tree-from-preorder-and-postorder-traversal].html

36 lines
1.4 KiB
HTML
Raw Normal View History

2022-03-27 20:46:41 +08:00
<p>给定两个整数数组,<code>preorder</code>&nbsp;<code>postorder</code> ,其中 <code>preorder</code> 是一个具有 <strong>无重复</strong> 值的二叉树的前序遍历,<code>postorder</code> 是同一棵树的后序遍历,重构并返回二叉树。</p>
<p>如果存在多个答案,您可以返回其中 <strong>任何</strong> 一个。</p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<p><img src="https://assets.leetcode.com/uploads/2021/07/24/lc-prepost.jpg" style="height: 265px; width: 304px;" /></p>
<pre>
<strong>输入:</strong>preorder = [1,2,4,5,3,6,7], postorder = [4,5,2,6,7,3,1]
<strong>输出:</strong>[1,2,3,4,5,6,7]
</pre>
<p><strong>示例 2:</strong></p>
<pre>
<strong>输入:</strong> preorder = [1], postorder = [1]
<strong>输出:</strong> [1]
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= preorder.length &lt;= 30</code></li>
<li><code>1 &lt;= preorder[i] &lt;= preorder.length</code></li>
<li><code>preorder</code>&nbsp;中所有值都 <strong>不同</strong></li>
<li><code>postorder.length == preorder.length</code></li>
<li><code>1 &lt;= postorder[i] &lt;= postorder.length</code></li>
<li><code>postorder</code>&nbsp;中所有值都 <strong>不同</strong></li>
<li>保证 <code>preorder</code>&nbsp;<code>postorder</code>&nbsp;是同一棵二叉树的前序遍历和后序遍历</li>
</ul>