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

Answer Posted / sanjeev sindhu

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



Check the boundary condition for p =NULL means no tree exists.
so depth should be -1 if only root node is there then depth
of the tree is 0.

Is This Answer Correct ?    13 Yes 14 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

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

3271


How do you verify if the two sentences/phrases input is an anagram using predefined functions in string.h and by using arrays?

2248


why nlogn is the lower limit of any sort algorithm?

2535


#include int main(void) { int a=4, b=2; a=b<>2 ; printf("%d",a); return 0; }

1287


How to palindrom string in c language?

9762






how to test pierrot divisor

2453


Write a routine to implement the polymarker function

4574


Design an implement of the inputs functions for event mode

3159


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.

2882


Sir... please give some important coding questions asked by product companies..

2014


how to create a 3x3 two dimensional array that will give you the sums on the left and bottom columns

3337


Given a spherical surface, write bump-mapping procedure to generate the bumpy surface of an orange

3067


Cluster head selection in Wireless Sensor Network using C programming language.

3368


Can you send Code for Run Length Encoding Of BMP Image in C Language in linux(i.e Compression and Decompression) ?

4066


write a function to give demostrate the functionality of 3d in 1d. function prototye: change(int value,int indexX,int indexY,int indexZ, int [] 1dArray); value=what is the date; indexX=x-asix indexY=y-axis indexZ=z-axis and 1dArray=in which and where the value is stored??

4379