mirror of
https://github.com/xlucn/PAT.git
synced 2026-11-05 06:37:35 +08:00
22 lines
3.6 KiB
HTML
22 lines
3.6 KiB
HTML
Counting Nodes in a BST
|
||
<div class="ques-view"><p>A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties:</p>
|
||
<ul>
|
||
<li>The left subtree of a node contains only nodes with keys less than or equal to the node's key.</li>
|
||
<li>The right subtree of a node contains only nodes with keys greater than the node's key.</li>
|
||
<li>Both the left and right subtrees must also be binary search trees.</li>
|
||
</ul>
|
||
<p>Insert a sequence of numbers into an initially empty binary search tree. Then you are supposed to count the total number of nodes in the lowest 2 levels of the resulting tree.</p>
|
||
<h3 id="input-specification-">Input Specification:</h3>
|
||
<p>Each input file contains one test case. For each case, the first line gives 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>1</mn><mn>0</mn><mn>0</mn><mn>0</mn></mrow>\le 1000</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">1</span><span class="mord mathrm">0</span><span class="mord mathrm">0</span><span class="mord mathrm">0</span></span></span></span>) which is the size of the input sequence. Then given in the next line are the <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> integers in <span class="katex"><span class="katex-mathml"><math><mrow><mo>[</mo><mo>−</mo><mn>1</mn><mn>0</mn><mn>0</mn><mn>0</mn><mn>1</mn><mn>0</mn><mn>0</mn><mn>0</mn><mo>]</mo></mrow>[-1000 1000]</math></span><span aria-hidden="true" class="katex-html"><span class="strut" style="height:0.75em;"></span><span class="strut bottom" style="height:1em;vertical-align:-0.25em;"></span><span class="base textstyle uncramped"><span class="mopen">[</span><span class="mord">−</span><span class="mord mathrm">1</span><span class="mord mathrm">0</span><span class="mord mathrm">0</span><span class="mord mathrm">0</span><span class="mord mathrm">1</span><span class="mord mathrm">0</span><span class="mord mathrm">0</span><span class="mord mathrm">0</span><span class="mclose">]</span></span></span></span> which are supposed to be inserted into an initially empty binary search tree.</p>
|
||
<h3 id="output-specification-">Output Specification:</h3>
|
||
<p>For each case, print in one line the numbers of nodes in the lowest 2 levels of the resulting tree in the format:</p>
|
||
<pre><code>n1 + n2 = n
|
||
</code></pre><p>where <code>n1</code> is the number of nodes in the lowest level, <code>n2</code> is that of the level above, and <code>n</code> is the sum.</p>
|
||
<h3 id="sample-input-">Sample Input:</h3>
|
||
<pre><code class="lang-in">9
|
||
25 30 42 16 20 20 35 -5 28
|
||
</code></pre>
|
||
<h3 id="sample-output-">Sample Output:</h3>
|
||
<pre><code class="lang-out">2 + 4 = 6
|
||
</code></pre>
|
||
</div> |