Not getting correct output
This is the correct url
@mohammadabdullahjawwad
there is mistake is your distance() function
- for base case if(root==NULL){ return -1000000; }. Return such distance which can never be possible.
- 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.