Doesn't get the mistake in my code


i am getting wrong answer in one test case. may you highlight my mistake in this code

There are 2 mistakes in your code. Check your ‘height()’ function once, the first mistake is in that function. The 2nd one is that when you are calculating the balance factor for current level i.e (lh-rh), it could be -2 also, but since your condition for sending true is (lh-rh)<=1, it will be wrong. You need to use Math.abs(lh-rh)<=1

i am not getting my mistake in height() function . it return an int value. by comparing left subtree and right subtree. may u explaing please.

int height= Math.max(lheight, rheight);

Here, you need to return:

Math.max(lheight, rheight) + 1;

This is because you need to add one for your current level as well.

1 Like