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 ?    19 Yes 14 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 ?    32 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 2 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 ?    4 Yes 3 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 ?    1 Yes 2 No
Hithere
 

 
 
 
Other C Code Interview Questions
 
  Question Asked @ Answers
 
How do you sort a Linked List (singly connected) in O(n) please mail to pawan.10k@gmail.com if u can find an anser...i m desperate to knw... Oracle3
#include<stdio.h> main() { int a[2][2][2] = { {10,2,3,4}, {5,6,7,8} }; int *p,*q; p=&a[2][2][2]; *q=***a; printf("%d----%d",*p,*q); }  1
main() { int i=5; printf("%d",++i++); }  1
struct Foo { char *pName; }; main() { struct Foo *obj = malloc(sizeof(struct Foo)); clrscr(); strcpy(obj->pName,"Your Name"); printf("%s", obj->pName); } a. Your Name b. compile error c. Name d. Runtime error HCL1
Is this code legal? int *ptr; ptr = (int *) 0x400;  1
main() { float me = 1.1; double you = 1.1; if(me==you) printf("I love U"); else printf("I hate U"); }  1
main() { register int a=2; printf("Address of a = %d",&a); printf("Value of a = %d",a); }  1
main() { int i = 0xff ; printf("\n%d", i<<2); } a. 4 b. 512 c. 1020 d. 1024 HCL1
main() { int c = 5; printf("%d", main||c); } a. 1 b. 5 c. 0 d. none of the above HCL1
#define prod(a,b) a*b main() { int x=3,y=4; printf("%d",prod(x+2,y-1)); }  1
main() { char c=' ',x,convert(z); getc(c); if((c>='a') && (c<='z')) x=convert(c); printf("%c",x); } convert(z) { return z-32; }  1
main() { char p[ ]="%d\n"; p[1] = 'c'; printf(p,65); }  1
How we print the table of 3 using for loop in c programing?  3
main() { char c; int i = 456; clrscr(); c = i; printf("%d", c); } a. 456 b. -456 c. random number d. none of the above HCL1
#include<stdio.h> void fun(int); int main() { int a; a=3; fun(a); printf("\n"); return 0; } void fun(int i) { if(n>0) { fun(--n); printf("%d",n); fun(--n); } } the answer is 0 1 2 0..someone explain how the code is executed..? Wipro1
main() { float i=1.5; switch(i) { case 1: printf("1"); case 2: printf("2"); default : printf("0"); } }  1
what will be the position of the file marker? a: fseek(ptr,0,SEEK_SET); b: fseek(ptr,0,SEEK_CUR);  1
#include<stdio.h> main() { char s[]={'a','b','c','\n','c','\0'}; char *p,*str,*str1; p=&s[3]; str=p; str1=s; printf("%d",++*p + ++*str1-32); }  1
typedef struct error{int warning, error, exception;}error; main() { error g1; g1.error =1; printf("%d",g1.error); }  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
 
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