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

Explain what is meant by 'bit masking'?

0 Answers  


what is bit rate & baud rate? plz give wave forms

0 Answers  


what is difference between ANSI structure and C99 Structure?

1 Answers   Wipro,


What is "Hungarian Notation"?

0 Answers   Celstream,


How is a pointer variable declared?

0 Answers  






What is wild pointer in c with example?

0 Answers  


what is the output of the program and explain why?? #include<stdio.h> void main ( ) { int k=4,j=0: switch (k) { case 3; j=300; case 4: j=400: case 5: j=500; } printf (ā€œ%d\nā€,j); }

14 Answers   Oracle,


What is #define in c?

0 Answers  


Write a C program to accept a matrix of any size. Find the frequency count of each element in the matrix and positions in which they appear in the matrix

0 Answers   CLG,


Explain the process of converting a Tree into a Binary Tree.

0 Answers   Ignou,


Is malloc memset faster than calloc?

0 Answers  


What are different types of pointers?

0 Answers  


Categories