mirror of
https://github.com/xlucn/PAT.git
synced 2026-11-05 06:37:35 +08:00
38 lines
2.5 KiB
HTML
38 lines
2.5 KiB
HTML
Is It a Complete AVL Tree
|
|
<div class="ques-view"><p>An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child subtrees of any node differ by at most one; if at any time they differ by more than one, rebalancing is done to restore this property. Figures 1-4 illustrate the rotation rules.</p>
|
|
<table>
|
|
<thead>
|
|
<tr>
|
|
<th><img alt="F1.jpg" src="https://images.ptausercontent.com/fb337acb-93b0-4af2-9838-deff5ce98058.jpg"/></th>
|
|
<th><img alt="F2.jpg" src="https://images.ptausercontent.com/d1635de7-3e3f-4aaa-889b-ba29f35890db.jpg"/></th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<tr>
|
|
<td><img alt="F3.jpg" src="https://images.ptausercontent.com/e868e4b9-9fea-4f70-b7a7-1f5d8a3be4ef.jpg"/></td>
|
|
<td><img alt="F4.jpg" src="https://images.ptausercontent.com/98aa1782-cea5-4792-8736-999436cf43a9.jpg"/></td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
<p>Now given a sequence of insertions, you are supposed to output the level-order traversal sequence of the resulting AVL tree, and to tell if it is a complete binary tree.</p>
|
|
<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 N (<span class="katex"><span class="katex-mathml"><math><mrow><mo>≤</mo></mrow>\le</math></span><span aria-hidden="true" class="katex-html"><span class="strut" style="height:0.63597em;"></span><span class="strut bottom" style="height:0.7719400000000001em;vertical-align:-0.13597em;"></span><span class="base textstyle uncramped"><span class="mrel">≤</span></span></span></span> 20). Then N distinct integer keys are given in the next line. All the numbers in a line are separated by a space.</p>
|
|
<h3 id="output-specification-">Output Specification:</h3>
|
|
<p>For each test case, insert the keys one by one into an initially empty AVL tree. Then first print in a line the level-order traversal sequence of the resulting AVL tree. All the numbers in a line must be separated by a space, and there must be no extra space at the end of the line. Then in the next line, print <code>YES</code> if the tree is complete, or <code>NO</code> if not.</p>
|
|
<h3 id="sample-input-1-">Sample Input 1:</h3>
|
|
<pre><code class="lang-in">5
|
|
88 70 61 63 65
|
|
</code></pre>
|
|
<h3 id="sample-output-1-">Sample Output 1:</h3>
|
|
<pre><code class="lang-out">70 63 88 61 65
|
|
YES
|
|
</code></pre>
|
|
<h3 id="sample-input-2-">Sample Input 2:</h3>
|
|
<pre><code class="lang-in">8
|
|
88 70 61 96 120 90 65 68
|
|
</code></pre>
|
|
<h3 id="sample-output-2-">Sample Output 2:</h3>
|
|
<pre><code class="lang-out">88 65 96 61 70 90 120 68
|
|
NO
|
|
</code></pre>
|
|
</div> |