Not getting correct output

This is the correct url

@mohammadabdullahjawwad
there is mistake is your distance() function

  1. for base case if(root==NULL){ return -1000000; }. Return such distance which can never be possible.
  2. line 122 should be return max(distance(root->left, p)+1, distance(root->right, p)+1);
    your code will work fine after these changes.

I made the said changes. The answer is coming out to be wrong. There’s some problem with the count variable. Please reply timely. https://ide.codingblocks.com/s/240547

@mohammadabdullahjawwad
Just initialise count=0; at the beginning of distance function.
Here is the updated code and it is working fine. Please don’t make unnecessary changes.

Thanks! Didn’t understand why we add 1 when returning. Also is it correct if we use the concept distance(lca, p) + distance(lca, q)

Understood the adding 1 part. Please reply to the other query

@mohammadabdullahjawwad
Yes you can do this. First find the lca then ans would be distance(lca, p) + distance(lca, q).
You can try this method too.

Thanks buddy @shrm.abhishek.kmr