Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I suspect that you want something like this:</p> <pre><code>size_t displayAscend(nodePtr p, int count) { size_t additional_count = 0; if (p-&gt;left != NULL) additional_count += displayAscend(p-&gt;left, count + additional_count); cout &lt;&lt; count + additional_count &lt;&lt; ". " &lt;&lt; p-&gt;acctNum &lt;&lt; endl; additional_count++; if (p-&gt;right != NULL) additional_count += displayAscend(p-&gt;right, count + additional_count); return additional_count; } </code></pre> <p>You can use <code>int</code> in place of <code>size_t</code> if you prefer.</p> <p>The reason is that each recursive call must return a count to its caller, for otherwise the caller cannot tell how many the recursive call has counted. The outermost caller, of course, can just discard the count if uninterested.</p> <p>Passing by reference is another way to do it, as another answer observes, though not my preferred way. (I personally prefer to implement <em>that</em> strategy with explicit pointers.)</p> <p>You ask whether making <code>count</code> a global variable would work. The answer is that, yes, it would <em>work</em> for the restricted purpose of your limited exercise, but it would represent abysmal programming practice. After all, what if you had several trees, each with its own count?</p> <p><em>Update:</em> Thanks to @JerryCoffin for pointing out the former error in my code. I have fixed it above. What is more, I have tested it with the following:</p> <pre><code>#include &lt;vector&gt; #include &lt;iostream&gt; using std::cout; using std::endl; struct node { node *left; node *right; int acctNum; }; typedef node *nodePtr; size_t displayAscend(nodePtr p, int count) { size_t additional_count = 0; if (p-&gt;left != NULL) additional_count += displayAscend(p-&gt;left, count + additional_count); cout &lt;&lt; count + additional_count &lt;&lt; ". " &lt;&lt; p-&gt;acctNum &lt;&lt; endl; additional_count++; if (p-&gt;right != NULL) additional_count += displayAscend(p-&gt;right, count + additional_count); return additional_count; } int main() { node head; node n1; node n2; node n11; node n21; node n22; head.left = &amp;n1; head.right = &amp;n2; n1 .left = &amp;n11; n1 .right = 0; n2 .left = &amp;n21; n2 .right = &amp;n22; n11 .left = 0; n11 .right = 0; n21 .left = 0; n21 .right = 0; n22 .left = 0; n22 .right = 0; n11 .acctNum = 100; n1 .acctNum = 202; head.acctNum = 300; n21 .acctNum = 400; n2 .acctNum = 500; n22 .acctNum = 600; displayAscend(&amp;head, 0); } </code></pre> <p>The output is</p> <pre><code>0. 100 1. 202 2. 300 3. 400 4. 500 5. 600 </code></pre> <p>So, it works.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload