Diameter of a Tree : Function Output

My function is slightly different from the one discussed in the video. I am not getting why the output with both of the functions are coming out to be the same. (With the inputs I’ve tried). If I’m wrong then what input should I try on? Also, what’s the difference between both of the functions.
Here’s the code

I’ve tried with every possible input but still the output is coming out to be the same. I think it is happening because the case is being handled by h1+h2 always and since it will be maximum always.

Hi @raghav6, it is not always necessary that the root node will be included in the diameter.
Consider the following input:
1 2 4 5 6 -1 -1 -1 -1 7 -1 8 -1 9 -1 -1 3 -1 -1
It gives different outputs for both functions.
Here is the tree:
diamter
If you consider the path through root, the answer will be 5, but actually it is 6 (I’ve marked it with red)

If your doubt still persists, reply to this thread, else mark it as resolved.

1 Like

Thank you! @Khushboo, I was looking up for such a test case, I think all the test cases that I applied must be having diameter passing through the root node. Which is why the output from both of the functions were same. Thus, the another function was only accounting for the height of the root node and not the diameter for the left and right subtree.

Yes @raghav6. If your doubt is resolved, please mark it as resolved.