mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-10 14:20:43 +08:00
deploy
This commit is contained in:
@@ -3453,7 +3453,7 @@
|
||||
<p>给定一个二叉树的前序遍历 <code>preorder</code> 和中序遍历 <code>inorder</code> ,请从中构建二叉树,返回二叉树的根节点。</p>
|
||||
</div>
|
||||
<p><img alt="构建二叉树的示例数据" src="../build_binary_tree_problem.assets/build_tree_example.png" /></p>
|
||||
<p align="center"> 图:构建二叉树的示例数据 </p>
|
||||
<p align="center"> 图 12-5 构建二叉树的示例数据 </p>
|
||||
|
||||
<h3 id="1">1. 判断是否为分治问题<a class="headerlink" href="#1" title="Permanent link">¶</a></h3>
|
||||
<p>原问题定义为从 <code>preorder</code> 和 <code>inorder</code> 构建二叉树。我们首先从分治的角度分析这道题:</p>
|
||||
@@ -3466,17 +3466,17 @@
|
||||
<p>根据以上分析,这道题是可以使用分治来求解的,但问题是:<strong>如何通过前序遍历 <code>preorder</code> 和中序遍历 <code>inorder</code> 来划分左子树和右子树呢</strong>?</p>
|
||||
<p>根据定义,<code>preorder</code> 和 <code>inorder</code> 都可以被划分为三个部分:</p>
|
||||
<ul>
|
||||
<li>前序遍历:<code>[ 根节点 | 左子树 | 右子树 ]</code> ,例如上图 <code>[ 3 | 9 | 2 1 7 ]</code> 。</li>
|
||||
<li>中序遍历:<code>[ 左子树 | 根节点 | 右子树 ]</code> ,例如上图 <code>[ 9 | 3 | 1 2 7 ]</code> 。</li>
|
||||
<li>前序遍历:<code>[ 根节点 | 左子树 | 右子树 ]</code> ,例如图 12-5 的树对应 <code>[ 3 | 9 | 2 1 7 ]</code> 。</li>
|
||||
<li>中序遍历:<code>[ 左子树 | 根节点 | 右子树 ]</code> ,例如图 12-5 的树对应 <code>[ 9 | 3 | 1 2 7 ]</code> 。</li>
|
||||
</ul>
|
||||
<p>以上图数据为例,我们可以通过下图所示的步骤得到划分结果:</p>
|
||||
<p>以上图数据为例,我们可以通过图 12-6 所示的步骤得到划分结果:</p>
|
||||
<ol>
|
||||
<li>前序遍历的首元素 3 是根节点的值。</li>
|
||||
<li>查找根节点 3 在 <code>inorder</code> 中的索引,利用该索引可将 <code>inorder</code> 划分为 <code>[ 9 | 3 | 1 2 7 ]</code> 。</li>
|
||||
<li>根据 <code>inorder</code> 划分结果,易得左子树和右子树的节点数量分别为 1 和 3 ,从而可将 <code>preorder</code> 划分为 <code>[ 3 | 9 | 2 1 7 ]</code> 。</li>
|
||||
</ol>
|
||||
<p><img alt="在前序和中序遍历中划分子树" src="../build_binary_tree_problem.assets/build_tree_preorder_inorder_division.png" /></p>
|
||||
<p align="center"> 图:在前序和中序遍历中划分子树 </p>
|
||||
<p align="center"> 图 12-6 在前序和中序遍历中划分子树 </p>
|
||||
|
||||
<h3 id="3">3. 基于变量描述子树区间<a class="headerlink" href="#3" title="Permanent link">¶</a></h3>
|
||||
<p>根据以上划分方法,<strong>我们已经得到根节点、左子树、右子树在 <code>preorder</code> 和 <code>inorder</code> 中的索引区间</strong>。而为了描述这些索引区间,我们需要借助几个指针变量:</p>
|
||||
@@ -3485,8 +3485,8 @@
|
||||
<li>将当前树的根节点在 <code>inorder</code> 中的索引记为 <span class="arithmatex">\(m\)</span> 。</li>
|
||||
<li>将当前树在 <code>inorder</code> 中的索引区间记为 <span class="arithmatex">\([l, r]\)</span> 。</li>
|
||||
</ul>
|
||||
<p>如下表所示,通过以上变量即可表示根节点在 <code>preorder</code> 中的索引,以及子树在 <code>inorder</code> 中的索引区间。</p>
|
||||
<p align="center"> 表:根节点和子树在前序和中序遍历中的索引 </p>
|
||||
<p>如表 12-1 所示,通过以上变量即可表示根节点在 <code>preorder</code> 中的索引,以及子树在 <code>inorder</code> 中的索引区间。</p>
|
||||
<p align="center"> 表 12-1 根节点和子树在前序和中序遍历中的索引 </p>
|
||||
|
||||
<div class="center-table">
|
||||
<table>
|
||||
@@ -3516,9 +3516,9 @@
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
<p>请注意,右子树根节点索引中的 <span class="arithmatex">\((m-l)\)</span> 的含义是“左子树的节点数量”,建议配合下图理解。</p>
|
||||
<p>请注意,右子树根节点索引中的 <span class="arithmatex">\((m-l)\)</span> 的含义是“左子树的节点数量”,建议配合图 12-7 理解。</p>
|
||||
<p><img alt="根节点和左右子树的索引区间表示" src="../build_binary_tree_problem.assets/build_tree_division_pointers.png" /></p>
|
||||
<p align="center"> 图:根节点和左右子树的索引区间表示 </p>
|
||||
<p align="center"> 图 12-7 根节点和左右子树的索引区间表示 </p>
|
||||
|
||||
<h3 id="4">4. 代码实现<a class="headerlink" href="#4" title="Permanent link">¶</a></h3>
|
||||
<p>为了提升查询 <span class="arithmatex">\(m\)</span> 的效率,我们借助一个哈希表 <code>hmap</code> 来存储数组 <code>inorder</code> 中元素到索引的映射。</p>
|
||||
@@ -3830,7 +3830,7 @@
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<p>下图展示了构建二叉树的递归过程,各个节点是在向下“递”的过程中建立的,而各条边(即引用)是在向上“归”的过程中建立的。</p>
|
||||
<p>图 12-8 展示了构建二叉树的递归过程,各个节点是在向下“递”的过程中建立的,而各条边(即引用)是在向上“归”的过程中建立的。</p>
|
||||
<div class="tabbed-set tabbed-alternate" data-tabs="2:10"><input checked="checked" id="__tabbed_2_1" name="__tabbed_2" type="radio" /><input id="__tabbed_2_2" name="__tabbed_2" type="radio" /><input id="__tabbed_2_3" name="__tabbed_2" type="radio" /><input id="__tabbed_2_4" name="__tabbed_2" type="radio" /><input id="__tabbed_2_5" name="__tabbed_2" type="radio" /><input id="__tabbed_2_6" name="__tabbed_2" type="radio" /><input id="__tabbed_2_7" name="__tabbed_2" type="radio" /><input id="__tabbed_2_8" name="__tabbed_2" type="radio" /><input id="__tabbed_2_9" name="__tabbed_2" type="radio" /><input id="__tabbed_2_10" name="__tabbed_2" type="radio" /><div class="tabbed-labels"><label for="__tabbed_2_1"><1></label><label for="__tabbed_2_2"><2></label><label for="__tabbed_2_3"><3></label><label for="__tabbed_2_4"><4></label><label for="__tabbed_2_5"><5></label><label for="__tabbed_2_6"><6></label><label for="__tabbed_2_7"><7></label><label for="__tabbed_2_8"><8></label><label for="__tabbed_2_9"><9></label><label for="__tabbed_2_10"><10></label></div>
|
||||
<div class="tabbed-content">
|
||||
<div class="tabbed-block">
|
||||
@@ -3865,7 +3865,7 @@
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<p align="center"> 图:构建二叉树的递归过程 </p>
|
||||
<p align="center"> 图 12-8 构建二叉树的递归过程 </p>
|
||||
|
||||
<p>设树的节点数量为 <span class="arithmatex">\(n\)</span> ,初始化每一个节点(执行一个递归函数 <code>dfs()</code> )使用 <span class="arithmatex">\(O(1)\)</span> 时间。<strong>因此总体时间复杂度为 <span class="arithmatex">\(O(n)\)</span></strong> 。</p>
|
||||
<p>哈希表存储 <code>inorder</code> 元素到索引的映射,空间复杂度为 <span class="arithmatex">\(O(n)\)</span> 。最差情况下,即二叉树退化为链表时,递归深度达到 <span class="arithmatex">\(n\)</span> ,使用 <span class="arithmatex">\(O(n)\)</span> 的栈帧空间。<strong>因此总体空间复杂度为 <span class="arithmatex">\(O(n)\)</span></strong> 。</p>
|
||||
|
||||
Reference in New Issue
Block a user