title: How to stop recursion?
body: <p>The code below is <code>to find the path in a tree that adds up to a given sum</code>... what I do here is <code>enqueue</code> all nodes' values into a array <code>path</code> and print it if the conditions satisfies recursively....</p>
  
  <pre><code>void checkSum(NODE* root, int path[], int len, int sum){
  
       if(root == NULL) return;
  
       path[len] = root-&gt;data;
       len++;
  
       if(sum - root-&gt;data == 0){ 
              sum -= root-&gt;data;
              cout&lt;&lt;"\nSum equals..."; 
              printPaths(path, len); 
       }
       else if(sum - root-&gt;data &gt; 0){
            sum -= root-&gt;data;
            checkSum(root-&gt;left, path, len, sum);    
            checkSum(root-&gt;right, path, len, sum);
       }else { return; }
  }
  </code></pre>
  
  <p>What I wanna know is that is there any other way that I can print the path(atleast one) without using any data structures ???</p>
  
  <p>Something lik this....</p>
  
  <pre><code>void checkSum_second(NODE* root, int sum){
  
       if(root == NULL) return;
  
       if(sum - root-&gt;data == 0) {  
            //do something
       }     
       else if(sum - root-&gt;data &gt; 0){
            sum -= root-&gt;data;                 
       }else return;
  
  
       checkSum_second(root-&gt;left, sum);
       checkSum_second(root-&gt;right, sum);     
       cout&lt;&lt;"\nvalue..."&lt;&lt;root-&gt;data;
  }
  </code></pre>
  
  <p>consider a tree like</p>
  
  <pre><code>           1
         2       3
      4    5   6   7
  </code></pre>
  
  <p></p>
  
  <p>if <code>sum = 7</code> the <code>checkSum_second(root-&gt;left, sum);</code> is executed thrice i.e, till node 4, and here whether can we stop everything and just print the stack (i.e empty it)....</p>