title: Search key in binary tree
body: <p>the problem that I have is that for certain keys my function works, but for some it doesn't. The function should return a pointer to the node with the <code>key == givenKey</code> or <code>NULL</code> if there is no element in tree with that key. This is the structure and below is the function.</p>
  <pre><code>typedef struct node
  {
      int key;
      struct node *l, *r;
  }NodeT;
  
  NodeT *search(NodeT *root, int givenKey)
  {
      if(root == NULL || root-&gt;key == givenKey)
          return root;
      search(root-&gt;l, givenKey);
      search(root-&gt;r, givenKey);
  }
  </code></pre>
  <p>This is the call of the function in main:</p>
  <pre><code>    NodeT *q = search(root, 9);
      NodeT *r = search(root, 7);
      printf(&quot;%p\n%p\n&quot;, q, r);
  </code></pre>
  <p>And this is the given binary tree: (for example <code>q</code> gets the right address, but <code>r</code> will be <code>NULL</code> even though there is an element with <code>key = 7</code> in our binary tree.</p>
  <p><a href="https://i.stack.imgur.com/cdtJk.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/cdtJk.png" alt="enter image description here" /></a></p>
  <p>I would really appreciate if you tell me what is wrong, because I tried to change the function a lot of times, but it didn't work. I even looked through my code with the debugger, but it doesn't seem to help that much. Thank you in advance!</p>