Write a function to find the depth of a binary tree.

Answers were Sorted based on User's Feedback



Write a function to find the depth of a binary tree...

Answer / shabana parveen

int depth(treenode *p)
{
if(p==NULL)return(0);
if(p->left){h1=depth(p->left);}
if(p=>right){h2=depth(p->right);}
return(max(h1,h2)+1);
}


dis is really a good program.

actually it is so efficient in time and to the point that i
hav copied it again from neetu katiyar.

Is This Answer Correct ?    31 Yes 34 No

Write a function to find the depth of a binary tree...

Answer / mohan p

int depth(treenode *p)
{
if(p==NULL)return(0);
if(p->left){h1=depth(p->left);}
if(p=>right){h2=depth(p->right);}
return(max(h1,h2)+1);
}

The above code will return the number of nodes in the
longest path from root to the leaf. So subtracting with -1
will give the depth of the tree. Depth of the tree is the
distance from root to the deepest leaf.

Is This Answer Correct ?    13 Yes 16 No

Write a function to find the depth of a binary tree...

Answer / sameera.adusumilli

int Depth(struct Node*node,int level)
{
if(Node!=NULL)
{
if(level<depth)
depth=level;
Depth(Node->leftchild,level+1);
Depth(Node->rightchild,level+1);
}
return(depth);
}

Is This Answer Correct ?    52 Yes 93 No

Post New Answer

More C Code Interview Questions

WAP to display 1,2,3,4,5........N

2 Answers  


main ( ) { static char *s[ ] = {“black”, “white”, “yellow”, “violet”}; char **ptr[ ] = {s+3, s+2, s+1, s}, ***p; p = ptr; **++p; printf(“%s”,*--*++p + 3); }

1 Answers  


Write a c program to search an element in an array using recursion

1 Answers   Wipro,


main() { while (strcmp(“some”,”some\0”)) printf(“Strings are not equal\n”); }

1 Answers  


main(int argc, char **argv) { printf("enter the character"); getchar(); sum(argv[1],argv[2]); } sum(num1,num2) int num1,num2; { return num1+num2; }

1 Answers  






What is the output for the following program main() { int arr2D[3][3]; printf("%d\n", ((arr2D==* arr2D)&&(* arr2D == arr2D[0])) ); }

1 Answers  


Find your day from your DOB?

15 Answers   Accenture, Microsoft,


write a program to find out roots of quadratic equation "x=-b+-(b^2-4ac0^-1/2/2a"

2 Answers  


Write a program that find and print how many odd numbers in a binary tree

1 Answers  


Hi, i have a project that the teacher want a pyramid of numbers in C# or java...when we click a button...the pyramid should be generated in a listbox/or JtextArea...and the pyramid should have the folowing form: 1 232 34543 4567654 567898765 67890109876 7890123210987 890123454321098 90123456765432109 0123456789876543210 Plz help with codes...didn't find anything on the net.

0 Answers  


could you please send the program code for multiplying sparse matrix in c????

0 Answers  


What are segment and offset addresses?

2 Answers   Infosys,


Categories