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                      
info       Did you received any Funny E-Mails from your Friends and like to share with rest of our friends? Yeah!! you can post that stuff   HERE
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 ?    17 Yes 12 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 ?    28 Yes 9 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 ?    5 Yes 9 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 ?    12 Yes 5 No
Prakashkumarng
 
  Re: Write a function to find the depth of a binary tree.
Answer
# 5
Simple way:

public int FindDepthOfTree(RBNode n)
        {
            if (n == null) return 0;
            return Math.Max(FindDepthOfTree(n.LeftNode), 
FindDepthOfTree(n.RightNode)) + 1;
        }
 
Is This Answer Correct ?    11 Yes 1 No
Raj
 
  Re: Write a function to find the depth of a binary tree.
Answer
# 6
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);
}

The above code will return the number of nodes in the
longest path from root to the leaf. So subtracting with -1
will give the depth of the tree. Depth of the tree is the
distance from root to the deepest leaf.
 
Is This Answer Correct ?    3 Yes 2 No
Mohan P
 
  Re: Write a function to find the depth of a binary tree.
Answer
# 7
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 ?    3 Yes 3 No
Sanjeev Sindhu
 
  Re: Write a function to find the depth of a binary tree.
Answer
# 8
int depth(treenode *p)
{
   if(p==NULL)return -1 ;
   int h1=depth(p->left);
   int h2=depth(p->right);
   return(max(h1,h2)+1);
}

in case where p has no child but is not NULL itself, it
should return 0.
 
Is This Answer Correct ?    0 Yes 2 No
Hithere
 

 
 
 
Other C Code Interview Questions
 
  Question Asked @ Answers
 
main(){ char a[100]; a[0]='a';a[1]]='b';a[2]='c';a[4]='d'; abc(a); } abc(char a[]){ a++; printf("%c",*a); a++; printf("%c",*a); }  1
main() { int k=1; printf("%d==1 is ""%s",k,k==1?"TRUE":"FALSE"); }  1
There were 10 records stored in “somefile.dat” but the following program printed 11 names. What went wrong? void main() { struct student { char name[30], rollno[6]; }stud; FILE *fp = fopen(“somefile.dat”,”r”); while(!feof(fp)) { fread(&stud, sizeof(stud), 1 , fp); puts(stud.name); } }  1
main() { int *j; { int i=10; j=&i; } printf("%d",*j); }  1
Which version do you prefer of the following two, 1) printf(“%s”,str); // or the more curt one 2) printf(str);  1
What is the output for the program given below typedef enum errorType{warning, error, exception,}error; main() { error g1; g1=1; printf("%d",g1); }  1
In the following pgm add a stmt in the function fun such that the address of 'a' gets stored in 'j'. main(){ int * j; void fun(int **); fun(&j); } void fun(int **k) { int a =0; /* add a stmt here*/ }  1
main() { int i=-1; -i; printf("i = %d, -i = %d \n",i,-i); }  1
void main() { static int i=i++, j=j++, k=k++; printf(“i = %d j = %d k = %d”, i, j, k); }  1
void main() { unsigned giveit=-1; int gotit; printf("%u ",++giveit); printf("%u \n",gotit=--giveit); }  1
main() { if ((1||0) && (0||1)) { printf("OK I am done."); } else { printf("OK I am gone."); } } a. OK I am done b. OK I am gone c. compile error d. none of the above HCL1
Is the following code legal? struct a { int x; struct a *b; }  1
main() { char *p="hai friends",*p1; p1=p; while(*p!='\0') ++*p++; printf("%s %s",p,p1); }  1
char *someFun() { char *temp = “string constant"; return temp; } int main() { puts(someFun()); }  1
# include<stdio.h> aaa() { printf("hi"); } bbb(){ printf("hello"); } ccc(){ printf("bye"); } main() { int (*ptr[3])(); ptr[0]=aaa; ptr[1]=bbb; ptr[2]=ccc; ptr[2](); }  1
main() { char *str1="abcd"; char str2[]="abcd"; printf("%d %d %d",sizeof(str1),sizeof(str2),sizeof("abcd")); }  1
union u { union u { int i; int j; }a[10]; int b[10]; }u; main() { printf("\n%d", sizeof(u)); printf(" %d", sizeof(u.a)); // printf("%d", sizeof(u.a[4].i)); } a. 4, 4, 4 b. 40, 4, 4 c. 1, 100, 1 d. 40 400 4 HCL1
void main() { printf(“sizeof (void *) = %d \n“, sizeof( void *)); printf(“sizeof (int *) = %d \n”, sizeof(int *)); printf(“sizeof (double *) = %d \n”, sizeof(double *)); printf(“sizeof(struct unknown *) = %d \n”, sizeof(struct unknown *)); }  1
main() { float f=5,g=10; enum{i=10,j=20,k=50}; printf("%d\n",++k); printf("%f\n",f<<2); printf("%lf\n",f%g); printf("%lf\n",fmod(f,g)); }  1
Write a C function to search a number in the given list of numbers. donot use printf and scanf Honeywell4
 
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