title: How can i maintain state between recursion calls
body: <p>Here is the question I am trying to solve:
  Find the kth smallest integer in a Binary Search Tree:</p>
  
  <p>My Algorithm: I will do an inorder traversal of the BST. Each time I visit a node, I will decrement k by 1 and when k=0, I should be in the kth smallest node.</p>
  
  <p>Here is my implementation:</p>
  
  <pre><code>void FindKthSmallest(struct TreeNode* root, int k)
  {
    if (root == NULL) return;
    if (k== 0) return;
    FindKthSmallest (root-&gt;left, k);
    k--;
    if (k == 0)
    {
      cout &lt;&lt; root-&gt;data;
      return;
    }
    FindKthSmallest (root-&gt;right, k);
  }
  </code></pre>
  
  <p>However with the above implementation, I see that the state of k can't be maintained between the recursive calls.</p>
  
  <p>I think the state of k need to be maintained in 2 scenarios: recursive call returns between child and parent and recursive calls between parent to child - This is where I am struggling. Is there a way to maintain the state in such a scenario ?</p>