A binary tree is said to be balanced if both of its subtrees are balanced and the height of its left subtree differs from the height of its right subtree by at most 1. Write a C function to determine whether a given binary tree is balanced.
Difficulty level
Video recording
This exercise is mostly suitable for students
int height(Btree B)
{
if(!B) return 0;
return 1+ max(height(B->left), height(B->right));
}
int balanced(Btree B)
{
if(!B) return 1;
return balanced(B->left) && balanced(B->right) && abs(height(B->right) - height(B->left))<=1;
}
Back to the list of exercises
Looking for a more challenging exercise, try this one !!
Finding a value in an array using jump search