Sei sulla pagina 1di 1

(d) a binary tree is said to be balanced if the differnce of the depth of the two

leaves (one is of min depth and other is of max depth) is one...


.write a function isbalanced(node *p) to find
if the tree is balanced .

int mindepth(node *p)


{
if(!p)
return -1;

return minimum{mindepth(p->left)+1,mindepth(p->right)+1};

int maxdepth(node *p)


{
if(!p)
return -1;

return maximum{maxdepth(p->left)+1,maxdepth(p->right)+1};

if(maxdepth(p)-mindepth(p)>1)

print"tree is not balanced"

else

print"tree is balanced";

complexity =o(n)

Potrebbero piacerti anche