mirror of
https://github.com/xlucn/PAT.git
synced 2026-11-04 00:38:13 +08:00
28 lines
3.2 KiB
HTML
28 lines
3.2 KiB
HTML
Tree Traversals Again
|
|
<div class="ques-view"><p>An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For example, suppose that when a 6-node binary tree (with the keys numbered from 1 to 6) is traversed, the stack operations are: push(1); push(2); push(3); pop(); pop(); push(4); pop(); pop(); push(5); push(6); pop(); pop(). Then a unique binary tree (shown in Figure 1) can be generated from this sequence of operations. Your task is to give the postorder traversal sequence of this tree.</p>
|
|
<center>
|
|
<img alt="" src="https://images.ptausercontent.com/30"/><br/>
|
|
Figure 1 </center>
|
|
<h3 id="input-specification-">Input Specification:</h3>
|
|
<p>Each input file contains one test case. For each case, the first line contains a positive integer <span class="katex"><span class="katex-mathml"><math><mrow><mi>N</mi></mrow>N</math></span><span aria-hidden="true" class="katex-html"><span class="strut" style="height:0.68333em;"></span><span class="strut bottom" style="height:0.68333em;vertical-align:0em;"></span><span class="base textstyle uncramped"><span class="mord mathit" style="margin-right:0.10903em;">N</span></span></span></span> (<span class="katex"><span class="katex-mathml"><math><mrow><mo>≤</mo><mn>3</mn><mn>0</mn></mrow>\le 30</math></span><span aria-hidden="true" class="katex-html"><span class="strut" style="height:0.64444em;"></span><span class="strut bottom" style="height:0.78041em;vertical-align:-0.13597em;"></span><span class="base textstyle uncramped"><span class="mrel">≤</span><span class="mord mathrm">3</span><span class="mord mathrm">0</span></span></span></span>) which is the total number of nodes in a tree (and hence the nodes are numbered from 1 to <span class="katex"><span class="katex-mathml"><math><mrow><mi>N</mi></mrow>N</math></span><span aria-hidden="true" class="katex-html"><span class="strut" style="height:0.68333em;"></span><span class="strut bottom" style="height:0.68333em;vertical-align:0em;"></span><span class="base textstyle uncramped"><span class="mord mathit" style="margin-right:0.10903em;">N</span></span></span></span>). Then <span class="katex"><span class="katex-mathml"><math><mrow><mn>2</mn><mi>N</mi></mrow>2N</math></span><span aria-hidden="true" class="katex-html"><span class="strut" style="height:0.68333em;"></span><span class="strut bottom" style="height:0.68333em;vertical-align:0em;"></span><span class="base textstyle uncramped"><span class="mord mathrm">2</span><span class="mord mathit" style="margin-right:0.10903em;">N</span></span></span></span> lines follow, each describes a stack operation in the format: "Push X" where X is the index of the node being pushed onto the stack; or "Pop" meaning to pop one node from the stack.</p>
|
|
<h3 id="output-specification-">Output Specification:</h3>
|
|
<p>For each test case, print the postorder traversal sequence of the corresponding tree in one line. A solution is guaranteed to exist. All the numbers must be separated by exactly one space, and there must be no extra space at the end of the line.</p>
|
|
<h3 id="sample-input-">Sample Input:</h3>
|
|
<pre><code class="lang-in">6
|
|
Push 1
|
|
Push 2
|
|
Push 3
|
|
Pop
|
|
Pop
|
|
Push 4
|
|
Pop
|
|
Pop
|
|
Push 5
|
|
Push 6
|
|
Pop
|
|
Pop
|
|
</code></pre>
|
|
<h3 id="sample-output-">Sample Output:</h3>
|
|
<pre><code class="lang-out">3 4 2 6 5 1
|
|
</code></pre>
|
|
</div> |