|
|
|
@ -3219,7 +3219,7 @@
|
|
|
|
|
<p class="admonition-title">例题一</p>
|
|
|
|
|
<p>给定一个二叉树,搜索并记录所有值为 <span class="arithmatex">\(7\)</span> 的节点,返回节点列表。</p>
|
|
|
|
|
</div>
|
|
|
|
|
<p><strong>解题思路</strong>:前序遍历这颗树,并判断当前节点的值是否为 <span class="arithmatex">\(7\)</span> ,若是则将该节点的值加入到结果列表 <code>res</code> 之中。</p>
|
|
|
|
|
<p>对于此题,我们前序遍历这颗树,并判断当前节点的值是否为 <span class="arithmatex">\(7\)</span> ,若是则将该节点的值加入到结果列表 <code>res</code> 之中。</p>
|
|
|
|
|
<div class="tabbed-set tabbed-alternate" data-tabs="1:11"><input checked="checked" id="__tabbed_1_1" name="__tabbed_1" type="radio" /><input id="__tabbed_1_2" name="__tabbed_1" type="radio" /><input id="__tabbed_1_3" name="__tabbed_1" type="radio" /><input id="__tabbed_1_4" name="__tabbed_1" type="radio" /><input id="__tabbed_1_5" name="__tabbed_1" type="radio" /><input id="__tabbed_1_6" name="__tabbed_1" type="radio" /><input id="__tabbed_1_7" name="__tabbed_1" type="radio" /><input id="__tabbed_1_8" name="__tabbed_1" type="radio" /><input id="__tabbed_1_9" name="__tabbed_1" type="radio" /><input id="__tabbed_1_10" name="__tabbed_1" type="radio" /><input id="__tabbed_1_11" name="__tabbed_1" type="radio" /><div class="tabbed-labels"><label for="__tabbed_1_1">Java</label><label for="__tabbed_1_2">C++</label><label for="__tabbed_1_3">Python</label><label for="__tabbed_1_4">Go</label><label for="__tabbed_1_5">JavaScript</label><label for="__tabbed_1_6">TypeScript</label><label for="__tabbed_1_7">C</label><label for="__tabbed_1_8">C#</label><label for="__tabbed_1_9">Swift</label><label for="__tabbed_1_10">Zig</label><label for="__tabbed_1_11">Dart</label></div>
|
|
|
|
|
<div class="tabbed-content">
|
|
|
|
|
<div class="tabbed-block">
|
|
|
|
@ -3364,7 +3364,7 @@
|
|
|
|
|
<p class="admonition-title">例题二</p>
|
|
|
|
|
<p>在二叉树中搜索所有值为 <span class="arithmatex">\(7\)</span> 的节点,<strong>返回根节点到这些节点的路径</strong>。</p>
|
|
|
|
|
</div>
|
|
|
|
|
<p><strong>解题思路</strong>:在例题一代码的基础上,我们需要借助一个列表 <code>path</code> 记录访问过的节点路径。当访问到值为 <span class="arithmatex">\(7\)</span> 的节点时,则复制 <code>path</code> 并添加进结果列表 <code>res</code> 。遍历完成后,<code>res</code> 中保存的就是所有的解。</p>
|
|
|
|
|
<p>在例题一代码的基础上,我们需要借助一个列表 <code>path</code> 记录访问过的节点路径。当访问到值为 <span class="arithmatex">\(7\)</span> 的节点时,则复制 <code>path</code> 并添加进结果列表 <code>res</code> 。遍历完成后,<code>res</code> 中保存的就是所有的解。</p>
|
|
|
|
|
<div class="tabbed-set tabbed-alternate" data-tabs="2:11"><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" /><input id="__tabbed_2_11" name="__tabbed_2" type="radio" /><div class="tabbed-labels"><label for="__tabbed_2_1">Java</label><label for="__tabbed_2_2">C++</label><label for="__tabbed_2_3">Python</label><label for="__tabbed_2_4">Go</label><label for="__tabbed_2_5">JavaScript</label><label for="__tabbed_2_6">TypeScript</label><label for="__tabbed_2_7">C</label><label for="__tabbed_2_8">C#</label><label for="__tabbed_2_9">Swift</label><label for="__tabbed_2_10">Zig</label><label for="__tabbed_2_11">Dart</label></div>
|
|
|
|
|
<div class="tabbed-content">
|
|
|
|
|
<div class="tabbed-block">
|
|
|
|
@ -3576,9 +3576,13 @@
|
|
|
|
|
<p>复杂的回溯问题通常包含一个或多个约束条件,<strong>约束条件通常可用于“剪枝”</strong>。</p>
|
|
|
|
|
<div class="admonition question">
|
|
|
|
|
<p class="admonition-title">例题三</p>
|
|
|
|
|
<p>在二叉树中搜索所有值为 <span class="arithmatex">\(7\)</span> 的节点,返回根节点到这些节点的路径,<strong>路径中不能包含值为 <span class="arithmatex">\(3\)</span> 的节点</strong>。</p>
|
|
|
|
|
<p>在二叉树中搜索所有值为 <span class="arithmatex">\(7\)</span> 的节点,返回根节点到这些节点的路径,<strong>路径中有且只有一个值为 <span class="arithmatex">\(7\)</span> 的节点,并且不能包含值为 <span class="arithmatex">\(3\)</span> 的节点</strong>。</p>
|
|
|
|
|
</div>
|
|
|
|
|
<p><strong>解题思路</strong>:在例题二的基础上添加剪枝操作,当遇到值为 <span class="arithmatex">\(3\)</span> 的节点时,则终止继续搜索。</p>
|
|
|
|
|
<p>在例题二的基础上添加剪枝操作,包括:</p>
|
|
|
|
|
<ul>
|
|
|
|
|
<li>当遇到值为 <span class="arithmatex">\(7\)</span> 的节点时,记录解并返回,终止搜索。</li>
|
|
|
|
|
<li>当遇到值为 <span class="arithmatex">\(3\)</span> 的节点时,则直接返回,停止继续搜索。</li>
|
|
|
|
|
</ul>
|
|
|
|
|
<div class="tabbed-set tabbed-alternate" data-tabs="4:11"><input checked="checked" id="__tabbed_4_1" name="__tabbed_4" type="radio" /><input id="__tabbed_4_2" name="__tabbed_4" type="radio" /><input id="__tabbed_4_3" name="__tabbed_4" type="radio" /><input id="__tabbed_4_4" name="__tabbed_4" type="radio" /><input id="__tabbed_4_5" name="__tabbed_4" type="radio" /><input id="__tabbed_4_6" name="__tabbed_4" type="radio" /><input id="__tabbed_4_7" name="__tabbed_4" type="radio" /><input id="__tabbed_4_8" name="__tabbed_4" type="radio" /><input id="__tabbed_4_9" name="__tabbed_4" type="radio" /><input id="__tabbed_4_10" name="__tabbed_4" type="radio" /><input id="__tabbed_4_11" name="__tabbed_4" type="radio" /><div class="tabbed-labels"><label for="__tabbed_4_1">Java</label><label for="__tabbed_4_2">C++</label><label for="__tabbed_4_3">Python</label><label for="__tabbed_4_4">Go</label><label for="__tabbed_4_5">JavaScript</label><label for="__tabbed_4_6">TypeScript</label><label for="__tabbed_4_7">C</label><label for="__tabbed_4_8">C#</label><label for="__tabbed_4_9">Swift</label><label for="__tabbed_4_10">Zig</label><label for="__tabbed_4_11">Dart</label></div>
|
|
|
|
|
<div class="tabbed-content">
|
|
|
|
|
<div class="tabbed-block">
|
|
|
|
@ -3593,12 +3597,13 @@
|
|
|
|
|
<a id="__codelineno-22-9" name="__codelineno-22-9" href="#__codelineno-22-9"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">root</span><span class="p">.</span><span class="na">val</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">7</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
|
|
|
|
<a id="__codelineno-22-10" name="__codelineno-22-10" href="#__codelineno-22-10"></a><span class="w"> </span><span class="c1">// 记录解</span>
|
|
|
|
|
<a id="__codelineno-22-11" name="__codelineno-22-11" href="#__codelineno-22-11"></a><span class="w"> </span><span class="n">res</span><span class="p">.</span><span class="na">add</span><span class="p">(</span><span class="k">new</span><span class="w"> </span><span class="n">ArrayList</span><span class="o"><></span><span class="p">(</span><span class="n">path</span><span class="p">));</span>
|
|
|
|
|
<a id="__codelineno-22-12" name="__codelineno-22-12" href="#__codelineno-22-12"></a><span class="w"> </span><span class="p">}</span>
|
|
|
|
|
<a id="__codelineno-22-13" name="__codelineno-22-13" href="#__codelineno-22-13"></a><span class="w"> </span><span class="n">preOrder</span><span class="p">(</span><span class="n">root</span><span class="p">.</span><span class="na">left</span><span class="p">);</span>
|
|
|
|
|
<a id="__codelineno-22-14" name="__codelineno-22-14" href="#__codelineno-22-14"></a><span class="w"> </span><span class="n">preOrder</span><span class="p">(</span><span class="n">root</span><span class="p">.</span><span class="na">right</span><span class="p">);</span>
|
|
|
|
|
<a id="__codelineno-22-15" name="__codelineno-22-15" href="#__codelineno-22-15"></a><span class="w"> </span><span class="c1">// 回退</span>
|
|
|
|
|
<a id="__codelineno-22-16" name="__codelineno-22-16" href="#__codelineno-22-16"></a><span class="w"> </span><span class="n">path</span><span class="p">.</span><span class="na">remove</span><span class="p">(</span><span class="n">path</span><span class="p">.</span><span class="na">size</span><span class="p">()</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mi">1</span><span class="p">);</span>
|
|
|
|
|
<a id="__codelineno-22-17" name="__codelineno-22-17" href="#__codelineno-22-17"></a><span class="p">}</span>
|
|
|
|
|
<a id="__codelineno-22-12" name="__codelineno-22-12" href="#__codelineno-22-12"></a><span class="w"> </span><span class="k">return</span><span class="p">;</span>
|
|
|
|
|
<a id="__codelineno-22-13" name="__codelineno-22-13" href="#__codelineno-22-13"></a><span class="w"> </span><span class="p">}</span>
|
|
|
|
|
<a id="__codelineno-22-14" name="__codelineno-22-14" href="#__codelineno-22-14"></a><span class="w"> </span><span class="n">preOrder</span><span class="p">(</span><span class="n">root</span><span class="p">.</span><span class="na">left</span><span class="p">);</span>
|
|
|
|
|
<a id="__codelineno-22-15" name="__codelineno-22-15" href="#__codelineno-22-15"></a><span class="w"> </span><span class="n">preOrder</span><span class="p">(</span><span class="n">root</span><span class="p">.</span><span class="na">right</span><span class="p">);</span>
|
|
|
|
|
<a id="__codelineno-22-16" name="__codelineno-22-16" href="#__codelineno-22-16"></a><span class="w"> </span><span class="c1">// 回退</span>
|
|
|
|
|
<a id="__codelineno-22-17" name="__codelineno-22-17" href="#__codelineno-22-17"></a><span class="w"> </span><span class="n">path</span><span class="p">.</span><span class="na">remove</span><span class="p">(</span><span class="n">path</span><span class="p">.</span><span class="na">size</span><span class="p">()</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mi">1</span><span class="p">);</span>
|
|
|
|
|
<a id="__codelineno-22-18" name="__codelineno-22-18" href="#__codelineno-22-18"></a><span class="p">}</span>
|
|
|
|
|
</code></pre></div>
|
|
|
|
|
</div>
|
|
|
|
|
<div class="tabbed-block">
|
|
|
|
@ -3613,12 +3618,13 @@
|
|
|
|
|
<a id="__codelineno-23-9" name="__codelineno-23-9" href="#__codelineno-23-9"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">root</span><span class="o">-></span><span class="n">val</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">7</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
|
|
|
|
<a id="__codelineno-23-10" name="__codelineno-23-10" href="#__codelineno-23-10"></a><span class="w"> </span><span class="c1">// 记录解</span>
|
|
|
|
|
<a id="__codelineno-23-11" name="__codelineno-23-11" href="#__codelineno-23-11"></a><span class="w"> </span><span class="n">res</span><span class="p">.</span><span class="n">push_back</span><span class="p">(</span><span class="n">path</span><span class="p">);</span>
|
|
|
|
|
<a id="__codelineno-23-12" name="__codelineno-23-12" href="#__codelineno-23-12"></a><span class="w"> </span><span class="p">}</span>
|
|
|
|
|
<a id="__codelineno-23-13" name="__codelineno-23-13" href="#__codelineno-23-13"></a><span class="w"> </span><span class="n">preOrder</span><span class="p">(</span><span class="n">root</span><span class="o">-></span><span class="n">left</span><span class="p">);</span>
|
|
|
|
|
<a id="__codelineno-23-14" name="__codelineno-23-14" href="#__codelineno-23-14"></a><span class="w"> </span><span class="n">preOrder</span><span class="p">(</span><span class="n">root</span><span class="o">-></span><span class="n">right</span><span class="p">);</span>
|
|
|
|
|
<a id="__codelineno-23-15" name="__codelineno-23-15" href="#__codelineno-23-15"></a><span class="w"> </span><span class="c1">// 回退</span>
|
|
|
|
|
<a id="__codelineno-23-16" name="__codelineno-23-16" href="#__codelineno-23-16"></a><span class="w"> </span><span class="n">path</span><span class="p">.</span><span class="n">pop_back</span><span class="p">();</span>
|
|
|
|
|
<a id="__codelineno-23-17" name="__codelineno-23-17" href="#__codelineno-23-17"></a><span class="p">}</span>
|
|
|
|
|
<a id="__codelineno-23-12" name="__codelineno-23-12" href="#__codelineno-23-12"></a><span class="w"> </span><span class="k">return</span><span class="p">;</span>
|
|
|
|
|
<a id="__codelineno-23-13" name="__codelineno-23-13" href="#__codelineno-23-13"></a><span class="w"> </span><span class="p">}</span>
|
|
|
|
|
<a id="__codelineno-23-14" name="__codelineno-23-14" href="#__codelineno-23-14"></a><span class="w"> </span><span class="n">preOrder</span><span class="p">(</span><span class="n">root</span><span class="o">-></span><span class="n">left</span><span class="p">);</span>
|
|
|
|
|
<a id="__codelineno-23-15" name="__codelineno-23-15" href="#__codelineno-23-15"></a><span class="w"> </span><span class="n">preOrder</span><span class="p">(</span><span class="n">root</span><span class="o">-></span><span class="n">right</span><span class="p">);</span>
|
|
|
|
|
<a id="__codelineno-23-16" name="__codelineno-23-16" href="#__codelineno-23-16"></a><span class="w"> </span><span class="c1">// 回退</span>
|
|
|
|
|
<a id="__codelineno-23-17" name="__codelineno-23-17" href="#__codelineno-23-17"></a><span class="w"> </span><span class="n">path</span><span class="p">.</span><span class="n">pop_back</span><span class="p">();</span>
|
|
|
|
|
<a id="__codelineno-23-18" name="__codelineno-23-18" href="#__codelineno-23-18"></a><span class="p">}</span>
|
|
|
|
|
</code></pre></div>
|
|
|
|
|
</div>
|
|
|
|
|
<div class="tabbed-block">
|
|
|
|
@ -3632,10 +3638,11 @@
|
|
|
|
|
<a id="__codelineno-24-8" name="__codelineno-24-8" href="#__codelineno-24-8"></a> <span class="k">if</span> <span class="n">root</span><span class="o">.</span><span class="n">val</span> <span class="o">==</span> <span class="mi">7</span><span class="p">:</span>
|
|
|
|
|
<a id="__codelineno-24-9" name="__codelineno-24-9" href="#__codelineno-24-9"></a> <span class="c1"># 记录解</span>
|
|
|
|
|
<a id="__codelineno-24-10" name="__codelineno-24-10" href="#__codelineno-24-10"></a> <span class="n">res</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="n">path</span><span class="p">))</span>
|
|
|
|
|
<a id="__codelineno-24-11" name="__codelineno-24-11" href="#__codelineno-24-11"></a> <span class="n">pre_order</span><span class="p">(</span><span class="n">root</span><span class="o">.</span><span class="n">left</span><span class="p">)</span>
|
|
|
|
|
<a id="__codelineno-24-12" name="__codelineno-24-12" href="#__codelineno-24-12"></a> <span class="n">pre_order</span><span class="p">(</span><span class="n">root</span><span class="o">.</span><span class="n">right</span><span class="p">)</span>
|
|
|
|
|
<a id="__codelineno-24-13" name="__codelineno-24-13" href="#__codelineno-24-13"></a> <span class="c1"># 回退</span>
|
|
|
|
|
<a id="__codelineno-24-14" name="__codelineno-24-14" href="#__codelineno-24-14"></a> <span class="n">path</span><span class="o">.</span><span class="n">pop</span><span class="p">()</span>
|
|
|
|
|
<a id="__codelineno-24-11" name="__codelineno-24-11" href="#__codelineno-24-11"></a> <span class="k">return</span>
|
|
|
|
|
<a id="__codelineno-24-12" name="__codelineno-24-12" href="#__codelineno-24-12"></a> <span class="n">pre_order</span><span class="p">(</span><span class="n">root</span><span class="o">.</span><span class="n">left</span><span class="p">)</span>
|
|
|
|
|
<a id="__codelineno-24-13" name="__codelineno-24-13" href="#__codelineno-24-13"></a> <span class="n">pre_order</span><span class="p">(</span><span class="n">root</span><span class="o">.</span><span class="n">right</span><span class="p">)</span>
|
|
|
|
|
<a id="__codelineno-24-14" name="__codelineno-24-14" href="#__codelineno-24-14"></a> <span class="c1"># 回退</span>
|
|
|
|
|
<a id="__codelineno-24-15" name="__codelineno-24-15" href="#__codelineno-24-15"></a> <span class="n">path</span><span class="o">.</span><span class="n">pop</span><span class="p">()</span>
|
|
|
|
|
</code></pre></div>
|
|
|
|
|
</div>
|
|
|
|
|
<div class="tabbed-block">
|
|
|
|
@ -3650,12 +3657,13 @@
|
|
|
|
|
<a id="__codelineno-25-9" name="__codelineno-25-9" href="#__codelineno-25-9"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="nb">int</span><span class="p">(</span><span class="nx">root</span><span class="p">.</span><span class="nx">Val</span><span class="p">)</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">7</span><span class="w"> </span><span class="p">{</span>
|
|
|
|
|
<a id="__codelineno-25-10" name="__codelineno-25-10" href="#__codelineno-25-10"></a><span class="w"> </span><span class="c1">// 记录解</span>
|
|
|
|
|
<a id="__codelineno-25-11" name="__codelineno-25-11" href="#__codelineno-25-11"></a><span class="w"> </span><span class="o">*</span><span class="nx">res</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="nb">append</span><span class="p">(</span><span class="o">*</span><span class="nx">res</span><span class="p">,</span><span class="w"> </span><span class="o">*</span><span class="nx">path</span><span class="p">)</span>
|
|
|
|
|
<a id="__codelineno-25-12" name="__codelineno-25-12" href="#__codelineno-25-12"></a><span class="w"> </span><span class="p">}</span>
|
|
|
|
|
<a id="__codelineno-25-13" name="__codelineno-25-13" href="#__codelineno-25-13"></a><span class="w"> </span><span class="nx">preOrderIII</span><span class="p">(</span><span class="nx">root</span><span class="p">.</span><span class="nx">Left</span><span class="p">,</span><span class="w"> </span><span class="nx">res</span><span class="p">,</span><span class="w"> </span><span class="nx">path</span><span class="p">)</span>
|
|
|
|
|
<a id="__codelineno-25-14" name="__codelineno-25-14" href="#__codelineno-25-14"></a><span class="w"> </span><span class="nx">preOrderIII</span><span class="p">(</span><span class="nx">root</span><span class="p">.</span><span class="nx">Right</span><span class="p">,</span><span class="w"> </span><span class="nx">res</span><span class="p">,</span><span class="w"> </span><span class="nx">path</span><span class="p">)</span>
|
|
|
|
|
<a id="__codelineno-25-15" name="__codelineno-25-15" href="#__codelineno-25-15"></a><span class="w"> </span><span class="c1">// 回退</span>
|
|
|
|
|
<a id="__codelineno-25-16" name="__codelineno-25-16" href="#__codelineno-25-16"></a><span class="w"> </span><span class="o">*</span><span class="nx">path</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="p">(</span><span class="o">*</span><span class="nx">path</span><span class="p">)[:</span><span class="nb">len</span><span class="p">(</span><span class="o">*</span><span class="nx">path</span><span class="p">)</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
|
|
|
|
|
<a id="__codelineno-25-17" name="__codelineno-25-17" href="#__codelineno-25-17"></a><span class="p">}</span>
|
|
|
|
|
<a id="__codelineno-25-12" name="__codelineno-25-12" href="#__codelineno-25-12"></a><span class="w"> </span><span class="k">return</span>
|
|
|
|
|
<a id="__codelineno-25-13" name="__codelineno-25-13" href="#__codelineno-25-13"></a><span class="w"> </span><span class="p">}</span>
|
|
|
|
|
<a id="__codelineno-25-14" name="__codelineno-25-14" href="#__codelineno-25-14"></a><span class="w"> </span><span class="nx">preOrderIII</span><span class="p">(</span><span class="nx">root</span><span class="p">.</span><span class="nx">Left</span><span class="p">,</span><span class="w"> </span><span class="nx">res</span><span class="p">,</span><span class="w"> </span><span class="nx">path</span><span class="p">)</span>
|
|
|
|
|
<a id="__codelineno-25-15" name="__codelineno-25-15" href="#__codelineno-25-15"></a><span class="w"> </span><span class="nx">preOrderIII</span><span class="p">(</span><span class="nx">root</span><span class="p">.</span><span class="nx">Right</span><span class="p">,</span><span class="w"> </span><span class="nx">res</span><span class="p">,</span><span class="w"> </span><span class="nx">path</span><span class="p">)</span>
|
|
|
|
|
<a id="__codelineno-25-16" name="__codelineno-25-16" href="#__codelineno-25-16"></a><span class="w"> </span><span class="c1">// 回退</span>
|
|
|
|
|
<a id="__codelineno-25-17" name="__codelineno-25-17" href="#__codelineno-25-17"></a><span class="w"> </span><span class="o">*</span><span class="nx">path</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="p">(</span><span class="o">*</span><span class="nx">path</span><span class="p">)[:</span><span class="nb">len</span><span class="p">(</span><span class="o">*</span><span class="nx">path</span><span class="p">)</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
|
|
|
|
|
<a id="__codelineno-25-18" name="__codelineno-25-18" href="#__codelineno-25-18"></a><span class="p">}</span>
|
|
|
|
|
</code></pre></div>
|
|
|
|
|
</div>
|
|
|
|
|
<div class="tabbed-block">
|
|
|
|
@ -3670,12 +3678,13 @@
|
|
|
|
|
<a id="__codelineno-26-9" name="__codelineno-26-9" href="#__codelineno-26-9"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="nx">root</span><span class="p">.</span><span class="nx">val</span><span class="w"> </span><span class="o">===</span><span class="w"> </span><span class="mf">7</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
|
|
|
|
<a id="__codelineno-26-10" name="__codelineno-26-10" href="#__codelineno-26-10"></a><span class="w"> </span><span class="c1">// 记录解</span>
|
|
|
|
|
<a id="__codelineno-26-11" name="__codelineno-26-11" href="#__codelineno-26-11"></a><span class="w"> </span><span class="nx">res</span><span class="p">.</span><span class="nx">push</span><span class="p">([...</span><span class="nx">path</span><span class="p">]);</span>
|
|
|
|
|
<a id="__codelineno-26-12" name="__codelineno-26-12" href="#__codelineno-26-12"></a><span class="w"> </span><span class="p">}</span>
|
|
|
|
|
<a id="__codelineno-26-13" name="__codelineno-26-13" href="#__codelineno-26-13"></a><span class="w"> </span><span class="nx">preOrder</span><span class="p">(</span><span class="nx">root</span><span class="p">.</span><span class="nx">left</span><span class="p">,</span><span class="w"> </span><span class="nx">path</span><span class="p">,</span><span class="w"> </span><span class="nx">res</span><span class="p">);</span>
|
|
|
|
|
<a id="__codelineno-26-14" name="__codelineno-26-14" href="#__codelineno-26-14"></a><span class="w"> </span><span class="nx">preOrder</span><span class="p">(</span><span class="nx">root</span><span class="p">.</span><span class="nx">right</span><span class="p">,</span><span class="w"> </span><span class="nx">path</span><span class="p">,</span><span class="w"> </span><span class="nx">res</span><span class="p">);</span>
|
|
|
|
|
<a id="__codelineno-26-15" name="__codelineno-26-15" href="#__codelineno-26-15"></a><span class="w"> </span><span class="c1">// 回退</span>
|
|
|
|
|
<a id="__codelineno-26-16" name="__codelineno-26-16" href="#__codelineno-26-16"></a><span class="w"> </span><span class="nx">path</span><span class="p">.</span><span class="nx">pop</span><span class="p">();</span>
|
|
|
|
|
<a id="__codelineno-26-17" name="__codelineno-26-17" href="#__codelineno-26-17"></a><span class="p">}</span>
|
|
|
|
|
<a id="__codelineno-26-12" name="__codelineno-26-12" href="#__codelineno-26-12"></a><span class="w"> </span><span class="k">return</span><span class="p">;</span>
|
|
|
|
|
<a id="__codelineno-26-13" name="__codelineno-26-13" href="#__codelineno-26-13"></a><span class="w"> </span><span class="p">}</span>
|
|
|
|
|
<a id="__codelineno-26-14" name="__codelineno-26-14" href="#__codelineno-26-14"></a><span class="w"> </span><span class="nx">preOrder</span><span class="p">(</span><span class="nx">root</span><span class="p">.</span><span class="nx">left</span><span class="p">,</span><span class="w"> </span><span class="nx">path</span><span class="p">,</span><span class="w"> </span><span class="nx">res</span><span class="p">);</span>
|
|
|
|
|
<a id="__codelineno-26-15" name="__codelineno-26-15" href="#__codelineno-26-15"></a><span class="w"> </span><span class="nx">preOrder</span><span class="p">(</span><span class="nx">root</span><span class="p">.</span><span class="nx">right</span><span class="p">,</span><span class="w"> </span><span class="nx">path</span><span class="p">,</span><span class="w"> </span><span class="nx">res</span><span class="p">);</span>
|
|
|
|
|
<a id="__codelineno-26-16" name="__codelineno-26-16" href="#__codelineno-26-16"></a><span class="w"> </span><span class="c1">// 回退</span>
|
|
|
|
|
<a id="__codelineno-26-17" name="__codelineno-26-17" href="#__codelineno-26-17"></a><span class="w"> </span><span class="nx">path</span><span class="p">.</span><span class="nx">pop</span><span class="p">();</span>
|
|
|
|
|
<a id="__codelineno-26-18" name="__codelineno-26-18" href="#__codelineno-26-18"></a><span class="p">}</span>
|
|
|
|
|
</code></pre></div>
|
|
|
|
|
</div>
|
|
|
|
|
<div class="tabbed-block">
|
|
|
|
@ -3694,12 +3703,13 @@
|
|
|
|
|
<a id="__codelineno-27-13" name="__codelineno-27-13" href="#__codelineno-27-13"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="nx">root</span><span class="p">.</span><span class="nx">val</span><span class="w"> </span><span class="o">===</span><span class="w"> </span><span class="mf">7</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
|
|
|
|
<a id="__codelineno-27-14" name="__codelineno-27-14" href="#__codelineno-27-14"></a><span class="w"> </span><span class="c1">// 记录解</span>
|
|
|
|
|
<a id="__codelineno-27-15" name="__codelineno-27-15" href="#__codelineno-27-15"></a><span class="w"> </span><span class="nx">res</span><span class="p">.</span><span class="nx">push</span><span class="p">([...</span><span class="nx">path</span><span class="p">]);</span>
|
|
|
|
|
<a id="__codelineno-27-16" name="__codelineno-27-16" href="#__codelineno-27-16"></a><span class="w"> </span><span class="p">}</span>
|
|
|
|
|
<a id="__codelineno-27-17" name="__codelineno-27-17" href="#__codelineno-27-17"></a><span class="w"> </span><span class="nx">preOrder</span><span class="p">(</span><span class="nx">root</span><span class="p">.</span><span class="nx">left</span><span class="p">,</span><span class="w"> </span><span class="nx">path</span><span class="p">,</span><span class="w"> </span><span class="nx">res</span><span class="p">);</span>
|
|
|
|
|
<a id="__codelineno-27-18" name="__codelineno-27-18" href="#__codelineno-27-18"></a><span class="w"> </span><span class="nx">preOrder</span><span class="p">(</span><span class="nx">root</span><span class="p">.</span><span class="nx">right</span><span class="p">,</span><span class="w"> </span><span class="nx">path</span><span class="p">,</span><span class="w"> </span><span class="nx">res</span><span class="p">);</span>
|
|
|
|
|
<a id="__codelineno-27-19" name="__codelineno-27-19" href="#__codelineno-27-19"></a><span class="w"> </span><span class="c1">// 回退</span>
|
|
|
|
|
<a id="__codelineno-27-20" name="__codelineno-27-20" href="#__codelineno-27-20"></a><span class="w"> </span><span class="nx">path</span><span class="p">.</span><span class="nx">pop</span><span class="p">();</span>
|
|
|
|
|
<a id="__codelineno-27-21" name="__codelineno-27-21" href="#__codelineno-27-21"></a><span class="p">}</span>
|
|
|
|
|
<a id="__codelineno-27-16" name="__codelineno-27-16" href="#__codelineno-27-16"></a><span class="w"> </span><span class="k">return</span><span class="p">;</span>
|
|
|
|
|
<a id="__codelineno-27-17" name="__codelineno-27-17" href="#__codelineno-27-17"></a><span class="w"> </span><span class="p">}</span>
|
|
|
|
|
<a id="__codelineno-27-18" name="__codelineno-27-18" href="#__codelineno-27-18"></a><span class="w"> </span><span class="nx">preOrder</span><span class="p">(</span><span class="nx">root</span><span class="p">.</span><span class="nx">left</span><span class="p">,</span><span class="w"> </span><span class="nx">path</span><span class="p">,</span><span class="w"> </span><span class="nx">res</span><span class="p">);</span>
|
|
|
|
|
<a id="__codelineno-27-19" name="__codelineno-27-19" href="#__codelineno-27-19"></a><span class="w"> </span><span class="nx">preOrder</span><span class="p">(</span><span class="nx">root</span><span class="p">.</span><span class="nx">right</span><span class="p">,</span><span class="w"> </span><span class="nx">path</span><span class="p">,</span><span class="w"> </span><span class="nx">res</span><span class="p">);</span>
|
|
|
|
|
<a id="__codelineno-27-20" name="__codelineno-27-20" href="#__codelineno-27-20"></a><span class="w"> </span><span class="c1">// 回退</span>
|
|
|
|
|
<a id="__codelineno-27-21" name="__codelineno-27-21" href="#__codelineno-27-21"></a><span class="w"> </span><span class="nx">path</span><span class="p">.</span><span class="nx">pop</span><span class="p">();</span>
|
|
|
|
|
<a id="__codelineno-27-22" name="__codelineno-27-22" href="#__codelineno-27-22"></a><span class="p">}</span>
|
|
|
|
|
</code></pre></div>
|
|
|
|
|
</div>
|
|
|
|
|
<div class="tabbed-block">
|
|
|
|
@ -3718,12 +3728,13 @@
|
|
|
|
|
<a id="__codelineno-29-9" name="__codelineno-29-9" href="#__codelineno-29-9"></a><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">root</span><span class="p">.</span><span class="n">val</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="m">7</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
|
|
|
|
|
<a id="__codelineno-29-10" name="__codelineno-29-10" href="#__codelineno-29-10"></a><span class="w"> </span><span class="c1">// 记录解</span>
|
|
|
|
|
<a id="__codelineno-29-11" name="__codelineno-29-11" href="#__codelineno-29-11"></a><span class="w"> </span><span class="n">res</span><span class="p">.</span><span class="n">Add</span><span class="p">(</span><span class="k">new</span><span class="w"> </span><span class="n">List</span><span class="o"><</span><span class="n">TreeNode</span><span class="o">></span><span class="p">(</span><span class="n">path</span><span class="p">));</span>
|
|
|
|
|
<a id="__codelineno-29-12" name="__codelineno-29-12" href="#__codelineno-29-12"></a><span class="w"> </span><span class="p">}</span>
|
|
|
|
|
<a id="__codelineno-29-13" name="__codelineno-29-13" href="#__codelineno-29-13"></a><span class="w"> </span><span class="n">preOrder</span><span class="p">(</span><span class="n">root</span><span class="p">.</span><span class="n">left</span><span class="p">);</span>
|
|
|
|
|
<a id="__codelineno-29-14" name="__codelineno-29-14" href="#__codelineno-29-14"></a><span class="w"> </span><span class="n">preOrder</span><span class="p">(</span><span class="n">root</span><span class="p">.</span><span class="n">right</span><span class="p">);</span>
|
|
|
|
|
<a id="__codelineno-29-15" name="__codelineno-29-15" href="#__codelineno-29-15"></a><span class="w"> </span><span class="c1">// 回退</span>
|
|
|
|
|
<a id="__codelineno-29-16" name="__codelineno-29-16" href="#__codelineno-29-16"></a><span class="w"> </span><span class="n">path</span><span class="p">.</span><span class="n">RemoveAt</span><span class="p">(</span><span class="n">path</span><span class="p">.</span><span class="n">Count</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="m">1</span><span class="p">);</span>
|
|
|
|
|
<a id="__codelineno-29-17" name="__codelineno-29-17" href="#__codelineno-29-17"></a><span class="p">}</span>
|
|
|
|
|
<a id="__codelineno-29-12" name="__codelineno-29-12" href="#__codelineno-29-12"></a><span class="w"> </span><span class="k">return</span><span class="p">;</span>
|
|
|
|
|
<a id="__codelineno-29-13" name="__codelineno-29-13" href="#__codelineno-29-13"></a><span class="w"> </span><span class="p">}</span>
|
|
|
|
|
<a id="__codelineno-29-14" name="__codelineno-29-14" href="#__codelineno-29-14"></a><span class="w"> </span><span class="n">preOrder</span><span class="p">(</span><span class="n">root</span><span class="p">.</span><span class="n">left</span><span class="p">);</span>
|
|
|
|
|
<a id="__codelineno-29-15" name="__codelineno-29-15" href="#__codelineno-29-15"></a><span class="w"> </span><span class="n">preOrder</span><span class="p">(</span><span class="n">root</span><span class="p">.</span><span class="n">right</span><span class="p">);</span>
|
|
|
|
|
<a id="__codelineno-29-16" name="__codelineno-29-16" href="#__codelineno-29-16"></a><span class="w"> </span><span class="c1">// 回退</span>
|
|
|
|
|
<a id="__codelineno-29-17" name="__codelineno-29-17" href="#__codelineno-29-17"></a><span class="w"> </span><span class="n">path</span><span class="p">.</span><span class="n">RemoveAt</span><span class="p">(</span><span class="n">path</span><span class="p">.</span><span class="n">Count</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="m">1</span><span class="p">);</span>
|
|
|
|
|
<a id="__codelineno-29-18" name="__codelineno-29-18" href="#__codelineno-29-18"></a><span class="p">}</span>
|
|
|
|
|
</code></pre></div>
|
|
|
|
|
</div>
|
|
|
|
|
<div class="tabbed-block">
|
|
|
|
@ -3738,12 +3749,13 @@
|
|
|
|
|
<a id="__codelineno-30-9" name="__codelineno-30-9" href="#__codelineno-30-9"></a> <span class="k">if</span> <span class="n">root</span><span class="p">.</span><span class="n">val</span> <span class="p">==</span> <span class="mi">7</span> <span class="p">{</span>
|
|
|
|
|
<a id="__codelineno-30-10" name="__codelineno-30-10" href="#__codelineno-30-10"></a> <span class="c1">// 记录解</span>
|
|
|
|
|
<a id="__codelineno-30-11" name="__codelineno-30-11" href="#__codelineno-30-11"></a> <span class="n">res</span><span class="p">.</span><span class="n">append</span><span class="p">(</span><span class="n">path</span><span class="p">)</span>
|
|
|
|
|
<a id="__codelineno-30-12" name="__codelineno-30-12" href="#__codelineno-30-12"></a> <span class="p">}</span>
|
|
|
|
|
<a id="__codelineno-30-13" name="__codelineno-30-13" href="#__codelineno-30-13"></a> <span class="n">preOrder</span><span class="p">(</span><span class="n">root</span><span class="p">:</span> <span class="n">root</span><span class="p">.</span><span class="kr">left</span><span class="p">)</span>
|
|
|
|
|
<a id="__codelineno-30-14" name="__codelineno-30-14" href="#__codelineno-30-14"></a> <span class="n">preOrder</span><span class="p">(</span><span class="n">root</span><span class="p">:</span> <span class="n">root</span><span class="p">.</span><span class="kr">right</span><span class="p">)</span>
|
|
|
|
|
<a id="__codelineno-30-15" name="__codelineno-30-15" href="#__codelineno-30-15"></a> <span class="c1">// 回退</span>
|
|
|
|
|
<a id="__codelineno-30-16" name="__codelineno-30-16" href="#__codelineno-30-16"></a> <span class="n">path</span><span class="p">.</span><span class="bp">removeLast</span><span class="p">()</span>
|
|
|
|
|
<a id="__codelineno-30-17" name="__codelineno-30-17" href="#__codelineno-30-17"></a><span class="p">}</span>
|
|
|
|
|
<a id="__codelineno-30-12" name="__codelineno-30-12" href="#__codelineno-30-12"></a> <span class="k">return</span>
|
|
|
|
|
<a id="__codelineno-30-13" name="__codelineno-30-13" href="#__codelineno-30-13"></a> <span class="p">}</span>
|
|
|
|
|
<a id="__codelineno-30-14" name="__codelineno-30-14" href="#__codelineno-30-14"></a> <span class="n">preOrder</span><span class="p">(</span><span class="n">root</span><span class="p">:</span> <span class="n">root</span><span class="p">.</span><span class="kr">left</span><span class="p">)</span>
|
|
|
|
|
<a id="__codelineno-30-15" name="__codelineno-30-15" href="#__codelineno-30-15"></a> <span class="n">preOrder</span><span class="p">(</span><span class="n">root</span><span class="p">:</span> <span class="n">root</span><span class="p">.</span><span class="kr">right</span><span class="p">)</span>
|
|
|
|
|
<a id="__codelineno-30-16" name="__codelineno-30-16" href="#__codelineno-30-16"></a> <span class="c1">// 回退</span>
|
|
|
|
|
<a id="__codelineno-30-17" name="__codelineno-30-17" href="#__codelineno-30-17"></a> <span class="n">path</span><span class="p">.</span><span class="bp">removeLast</span><span class="p">()</span>
|
|
|
|
|
<a id="__codelineno-30-18" name="__codelineno-30-18" href="#__codelineno-30-18"></a><span class="p">}</span>
|
|
|
|
|
</code></pre></div>
|
|
|
|
|
</div>
|
|
|
|
|
<div class="tabbed-block">
|
|
|
|
|