title: Finding height of BST for any node
body: <p>I was trying out code to find height of binary seach tree.I wrotte the code below:</p>
  
  <pre><code> int height(node *root)
  {
    if(root=NULL)
    return -1;
  
    int left=height(root-&gt;left);
    int right=height(root-&gt;right);
    return max(left,right)+1;
  }
  </code></pre>
  
  <p>I inserted the nodes 5,17,34 and 2 into the tree and passed the root to height function.
  Unexpectedly,the program got crashed and I had to force close the window during windows error reporting(I was trying in a IDE).I also tried this out in a online compiler but it also displayed runtime error.</p>
  
  <p>the MAIN function:</p>
  
  <pre><code>  int main()
  {
    node *root=new node();
    root=NULL;
    root=insert(root,5);
    root=insert(root,17);
    root=insert(root,34);
    root=insert(root,2);
    int x=height(root);
  }
  </code></pre>
  
  <p>the INSERT function if in case you want to look at it</p>
  
  <pre><code>     node *insert(node *root,int d)
      {
        if(root==NULL)
        {
         node *temp=new node();
         temp-&gt;data=d;
         temp-&gt;left=temp-&gt;right=NULL;
         root=temp;
  
        }
       else
      {  if(root-&gt;data&gt;d)
        {
         root-&gt;left=insert(root-&gt;left,d);
        }
        else
        root-&gt;right=insert(root-&gt;right,d);
      }
      return root;
     }
  </code></pre>
  
  <p>I want to mention that the problem started only when I tried to use the height function in the program.Before that,all the other functions had compiled and run successfully.I have tried dry run but cant figure out the problem. </p>