title: Searching an unordered binary tree by key value
body: <p>I posted a question related to this topic earlier, but I'm having trouble figuring this out as well. I'm trying to search an unordered binary tree by a key value and return its associated value through a recursive function. </p>
  
  <p>The class has the form:</p>
  
  <pre><code>  Class Node
    {
       private:
         Node *leftChild;
         Node *rightChild;
         int key;
         int value;
   }
  </code></pre>
  
  <p>With each variable having associated get methods. So I basically want to search through the binary tree and return its value once I get to the correct Node.</p>
  
  <p>Here is my attempt thus far, I think I'm pretty close:</p>
  
  <pre><code>int preOrder(Node *node, int key)
  {
     if(node-&gt;getKey() == key)
       return node-&gt;getValue();
  
    Node* leftNode = node-&gt;getLeft();
  
    if(leftNode != NULL)
    {
      return preOrder(leftNode, key);
    }
  
    Node* rightNode = node-&gt;getRight();
  
    if(rightNode != NULL)
    {
      return preOrder(rightNode, key);
    }
  
    //I know a return statement needs to be placed here
    //in case both pointers are NULL in order to return to the previous
    //node in the tree, but I'm not sure how to do this...
  }
  </code></pre>
  
  <p>Does anyone have any advice? </p>