ALLInterview.com :: Home Page KalAajKal.com
 Advertise your Business Here     
Browse  |   Placement Papers  |   Company  |   Code Snippets  |   Certifications  |   Visa Questions
Post Question  |   Post Answer  |   My Panel  |   Search  |   Articles  |   Topics  |   ERRORS new
   Refer this Site  Refer This Site to Your Friends  Site Map  Bookmark this Site  Set it as your HomePage  Contact Us     Login  |  Sign Up                      
Do you have a collection of Interview Questions and interested to share with us!!
Please send that collection to along with your userid / name. ThanQ
Google
 
Categories  >>  Code Snippets  >>  Programming Code  >>  C Code
 
 


 

 
 C Code interview questions  C Code Interview Questions
 C++ Code interview questions  C++ Code Interview Questions
 VC++ Code interview questions  VC++ Code Interview Questions
 Java Code interview questions  Java Code Interview Questions
 Dot Net Code interview questions  Dot Net Code Interview Questions
 Visual Basic Code interview questions  Visual Basic Code Interview Questions
 Programming Code AllOther interview questions  Programming Code AllOther Interview Questions
Question
Write a function to find the depth of a binary tree.
 Question Submitted By :: =-PKG-=
I also faced this Question!!     Rank Answer Posted By  
 
  Re: Write a function to find the depth of a binary tree.
Answer
# 1
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 ?    5 Yes 1 No
Sameera.adusumilli
 
  Re: Write a function to find the depth of a binary tree.
Answer
# 2
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);
}
 
Is This Answer Correct ?    8 Yes 2 No
Neetu Katiyar
 
 
 
  Re: Write a function to find the depth of a binary tree.
Answer
# 3
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 ?    0 Yes 3 No
Shabana Parveen
 
  Re: Write a function to find the depth of a binary tree.
Answer
# 4
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
typedef struct linkedlist
{
	int data;
	struct linkedlist *left,*right;
}node;
node *makenode(int);
void setleft(node *,int);
void setright(node *,int);
void preorder(node *);
void postorder(node *);
void inorder(node *);
int depth(node *,int);
node *q,*k;
int l=0,d=1;
void main()
{
	node *root,*p,*q;
	int x,f;
	clrscr();
	printf("\nEnter the root :");
	scanf("%d",&x);
	root=makenode(x);
	printf("\nEnter the data(-1 to stop) :");
	scanf("%d",&x);
	while(x!=-1)
	{
		p=root;
		while(p!=NULL)
		{
			q=p;
			if(x<p->data)
			{
				p=p->left;
			}
			else
			{
				p=p->right;
			}
		}
		if(x<q->data)
		{
			setleft(q,x);
		}
		else
		{
			setright(q,x);
		}
		printf("\nEnter the data(-1 to stop) :");
		scanf("%d",&x);
	}
	printf("\nPreorder traversal is \n");
	preorder(root);
	printf("\nPostorder traversal is \n");
	postorder(root);
	printf("\nInorder traversal is \n");
	inorder(root);
	printf("\nDepth of the tree is \n");
	f=depth(root,l);
	printf("%d",f+1);
	getch();
}
node *makenode(int x)
{
	node * temp;
	temp=(node *)malloc(sizeof(node));
	temp->data=x;
	temp->left=NULL;
	temp->right=NULL;
	return(temp);
}
void setleft(node *p,int x)
{
	node *temp;
	if(p==NULL)
	{
		printf("Error");
	}
	temp=makenode(x);
	p->left=temp;
}
void setright(node *p,int x)
{
	node *temp;
	if(p==NULL)
	{
		printf("Error");
	}
	temp=makenode(x);
	p->right=temp;
}
void inorder(node *p)
{
	if(p!=NULL)
	{
		inorder(p->left);
		printf("%d\t",p->data);
		inorder(p->right);
	}
}
void postorder(node *p)
{
	if(p!=NULL)
	{
		postorder(p->left);
		postorder(p->right);
		printf("%d\t",p->data);
	}
}
void preorder(node *p)
{
	if(p!=NULL)
	{
		printf("%d\t",p->data);
		preorder(p->left);
		preorder(p->right);
	}
}
int depth(node *p,int l)
{
	if(p!=NULL)
	{
		if(l>d)
		d=l;
		depth(p->left,l+1);
		depth(p->right,l+1);
	}
	return d;
}
 
Is This Answer Correct ?    1 Yes 1 No
Prakashkumarng
 

 
 
 
Other C Code Interview Questions
 
  Question Asked @ Answers
 
Give a oneline C expression to test whether a number is a power of 2? Motorola9
1) int i=5; j=i++ + i++ + i++; printf("%d",j);This code gives the answer 15.But if we replace the value of the j then anser is different?why? 2)int i=5; printf("%d",i++ + i++ + i++); this givs 18. Infosys3
How will u find whether a linked list has a loop or not? Microsoft3
Write a C function to search a number in the given list of numbers. donot use printf and scanf Honeywell4
plz send me all data structure related programs  2
How to reverse a String without using C functions ? Wipro8
Write a routine that prints out a 2-D array in spiral order Microsoft2
How to swap two variables, without using third variable ? HCL28
how can u draw a rectangle in C Wipro24
how to return a multiple value from a function? Wipro4
print a semicolon using Cprogram without using a semicolon any where in the C code in ur program!! Tata-Elxsi14
What is "far" and "near" pointers in "c"...?  2
plz send me all data structure related programs  1
Give a very good method to count the number of ones in a 32 bit number. (caution: looping through testing each bit is not a solution) Microsoft4
How to read a directory in a C program?  3
Write a routine to draw a circle (x ** 2 + y ** 2 = r ** 2) without making use of any floating point computations at all. Microsoft2
how to return a multiple value from a function? Wipro1
Finding a number multiplication of 8 with out using arithmetic operator NetApp7
Given only putchar (no sprintf, itoa, etc.) write a routine putlong that prints out an unsigned long in decimal.  5
Sorting entire link list using selection sort and insertion sort and calculating their time complexity NetApp1
 
For more C Code Interview Questions Click Here 
 
 
 
 
 
   
Copyright Policy  |  Terms of Service  |  Help  |  Site Map 1  |  Articles  |  Site Map  |   Site Map  |  Contact Us interview questions urls   External Links 
   
Copyright © 2007  ALLInterview.com.  All Rights Reserved.

ALLInterview.com   ::  Forum9.com   ::  KalAajKal.com