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

Can the sizeof operator be used to tell the size of an array passed to a function?

0 Answers  


actually i have 2 years teaching experience as computer faculty but now i am a DBA but when i go for interview many peoples asked me why i left my teaching profession and why i want to come in this field kindly give me the proper answer of this queston

3 Answers   Ramco,


What are the types of functions in c?

0 Answers  


An organised method of depicting the use of an area of computer memory used to signify the uses for different parts of the memory a) swap b) extended memory c) memory map d) all of the above

0 Answers  


program that accepts amount in figures and print that in words

2 Answers   Infosys, Lovely Professional University, Wipro,


What is difference between static and global variable in c?

0 Answers  


What is the size of empty structure in c?

0 Answers  


What is the method to save data in stack data structure type?

0 Answers  


If a variable is a pointer to a structure, then which operator is used to access data members of the structure through the pointer variable?

0 Answers  


How many types of errors are there in c language? Explain

0 Answers  


Device an algorithm for weiler-atherton polygon clipping, where the clipping window can be any specified polygon

0 Answers  


What are predefined functions in c?

0 Answers  


Categories