this my code, getting wrong output kindly rectify it
Is_Balanced ( Binary Tree )
@Legendankit hey apki recursion me thodi si problem hai ,see this pseudo code:
bool isBalanced(node* root)
{
int lh; /* for height of left subtree /
int rh; / for height of right subtree */
/* If tree is empty then return true */
if (root == NULL)
return 1;
/* Get the height of left and right sub trees */
lh = height(root->left);
rh = height(root->right);
if (abs(lh - rh) <= 1 && isBalanced(root->left) && isBalanced(root->right))
return 1;
/* If we reach here then
tree is not height-balanced */
return 0;
}
I did not get can help me in understanding using the code
@Legendankit hey basically for tree to be balanced every node ki ye tenn conditions satisfy honi chyy:
1: abs of height of left subtree -height of right subtrss <=1
2:left subtree should be balance
3.right subtree should be balance
So I have made height function and calculated height of right and height of left subtree and check the above three conditions ,if satisfied than tree is balanced otherwise not.
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.