Write code for finding depth of tree

Answers were Sorted based on User's Feedback



Write code for finding depth of tree..

Answer / om

struct tree //creating structure

{

int data; //data field of node

struct tree *lchild,*rchild;//left child & right child of node

};


//for depth calculation

int depth(struct tree *p)

{

int l,r;

if(p!=NULL)

{

l=depth(p->lchild);

r=depth(p->rchild);

return (1+((l>r)?l:r));

}

return -1;

}

Is This Answer Correct ?    6 Yes 0 No

Write code for finding depth of tree..

Answer / crispin

/*
* Simple tree node representation
*/
struct node_t {
struct node_t *left;
struct note_t *right;
};

/*
* Return the maximum depth of the tree given a pointer
* to its root node.
*/
unsigned int
tree_depth (node_t *root)
{
return (NULL == root) ? 0 :
MAX(tree_depth(root->left, root->right)+1);
}

Is This Answer Correct ?    3 Yes 6 No

Post New Answer

More C Interview Questions

write the function int countchtr(char string[],int ch);which returns the number of timesthe character ch appears in the string. for example the call countchtr("she lives in Newyork",'e') would return 3.

6 Answers  


C program to read the integer and calculate sum and average using single dimensional array

0 Answers  


What is dynamic memory allocation?

0 Answers  


What is the difference between fread buffer() and fwrite buffer()?

0 Answers  


What is || operator and how does it function in a program?

0 Answers  






parkside's triangle.. create a program like this.. enter the size: 6 enter the seed: 1 output: 1 23 456 7891 23456 789123 sample2: enter the size: 5 enter the seed: 3 output: 3 45 678 9123 45678 parkside should not exceed 10 while its seed should only be not more than 9..

4 Answers  


Difference between fopen() and open()?

3 Answers   Aricent,


What is the difference between getch() and getche()?

1 Answers   NSPL,


why we use "include" word before calling the header file. is there any special name for that include??????

1 Answers   TCS,


without using arithmatic operator convert an intger variable x into x+1

3 Answers  


pick out the odd one out of the following a.malloc() b.calloc() c.free() d.realloc()

2 Answers   TCS, ZenQ,


 Illustrate it   summing the series 2+4+6+......to n terms using  (i) while loop (ii) do-while loop

2 Answers  


Categories