|
|
|
@ -2528,7 +2528,45 @@
|
|
|
|
|
</div>
|
|
|
|
|
<div class="admonition note">
|
|
|
|
|
<p class="admonition-title">Note</p>
|
|
|
|
|
<p>使用循环一样可以实现前、中、后序遍历,但代码相对繁琐,有兴趣的同学可以自行实现。</p>
|
|
|
|
|
<p>我们也可以仅基于循环实现前、中、后序遍历,有兴趣的同学可以自行实现。</p>
|
|
|
|
|
</div>
|
|
|
|
|
<p>递归过程可分为“递”和“归”两个相反的部分。“递”表示开启新方法,程序在此过程中访问下一个节点;“归”表示函数返回,代表该节点已经访问完毕。如下图所示,为前序遍历二叉树的递归过程。</p>
|
|
|
|
|
<div class="tabbed-set tabbed-alternate" data-tabs="3:11"><input checked="checked" id="__tabbed_3_1" name="__tabbed_3" type="radio" /><input id="__tabbed_3_2" name="__tabbed_3" type="radio" /><input id="__tabbed_3_3" name="__tabbed_3" type="radio" /><input id="__tabbed_3_4" name="__tabbed_3" type="radio" /><input id="__tabbed_3_5" name="__tabbed_3" type="radio" /><input id="__tabbed_3_6" name="__tabbed_3" type="radio" /><input id="__tabbed_3_7" name="__tabbed_3" type="radio" /><input id="__tabbed_3_8" name="__tabbed_3" type="radio" /><input id="__tabbed_3_9" name="__tabbed_3" type="radio" /><input id="__tabbed_3_10" name="__tabbed_3" type="radio" /><input id="__tabbed_3_11" name="__tabbed_3" type="radio" /><div class="tabbed-labels"><label for="__tabbed_3_1"><1></label><label for="__tabbed_3_2"><2></label><label for="__tabbed_3_3"><3></label><label for="__tabbed_3_4"><4></label><label for="__tabbed_3_5"><5></label><label for="__tabbed_3_6"><6></label><label for="__tabbed_3_7"><7></label><label for="__tabbed_3_8"><8></label><label for="__tabbed_3_9"><9></label><label for="__tabbed_3_10"><10></label><label for="__tabbed_3_11"><11></label></div>
|
|
|
|
|
<div class="tabbed-content">
|
|
|
|
|
<div class="tabbed-block">
|
|
|
|
|
<p><img alt="preorder_step1" src="../binary_tree_traversal.assets/preorder_step1.png" /></p>
|
|
|
|
|
</div>
|
|
|
|
|
<div class="tabbed-block">
|
|
|
|
|
<p><img alt="preorder_step2" src="../binary_tree_traversal.assets/preorder_step2.png" /></p>
|
|
|
|
|
</div>
|
|
|
|
|
<div class="tabbed-block">
|
|
|
|
|
<p><img alt="preorder_step3" src="../binary_tree_traversal.assets/preorder_step3.png" /></p>
|
|
|
|
|
</div>
|
|
|
|
|
<div class="tabbed-block">
|
|
|
|
|
<p><img alt="preorder_step4" src="../binary_tree_traversal.assets/preorder_step4.png" /></p>
|
|
|
|
|
</div>
|
|
|
|
|
<div class="tabbed-block">
|
|
|
|
|
<p><img alt="preorder_step5" src="../binary_tree_traversal.assets/preorder_step5.png" /></p>
|
|
|
|
|
</div>
|
|
|
|
|
<div class="tabbed-block">
|
|
|
|
|
<p><img alt="preorder_step6" src="../binary_tree_traversal.assets/preorder_step6.png" /></p>
|
|
|
|
|
</div>
|
|
|
|
|
<div class="tabbed-block">
|
|
|
|
|
<p><img alt="preorder_step7" src="../binary_tree_traversal.assets/preorder_step7.png" /></p>
|
|
|
|
|
</div>
|
|
|
|
|
<div class="tabbed-block">
|
|
|
|
|
<p><img alt="preorder_step8" src="../binary_tree_traversal.assets/preorder_step8.png" /></p>
|
|
|
|
|
</div>
|
|
|
|
|
<div class="tabbed-block">
|
|
|
|
|
<p><img alt="preorder_step9" src="../binary_tree_traversal.assets/preorder_step9.png" /></p>
|
|
|
|
|
</div>
|
|
|
|
|
<div class="tabbed-block">
|
|
|
|
|
<p><img alt="preorder_step10" src="../binary_tree_traversal.assets/preorder_step10.png" /></p>
|
|
|
|
|
</div>
|
|
|
|
|
<div class="tabbed-block">
|
|
|
|
|
<p><img alt="preorder_step11" src="../binary_tree_traversal.assets/preorder_step11.png" /></p>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
<h3 id="_4">复杂度分析<a class="headerlink" href="#_4" title="Permanent link">¶</a></h3>
|
|
|
|
|
<p><strong>时间复杂度</strong>:所有节点被访问一次,使用 <span class="arithmatex">\(O(n)\)</span> 时间,其中 <span class="arithmatex">\(n\)</span> 为节点数量。</p>
|
|
|
|
|