Write a function to find the depth of a binary tree.

Answers were Sorted based on User's Feedback



Write a function to find the depth of a binary tree...

Answer / neetu katiyar

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 ?    159 Yes 47 No

Write a function to find the depth of a binary tree...

Answer / raj

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 ?    56 Yes 9 No

Write a function to find the depth of a binary tree...

Answer / prakashkumarng

#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 ?    48 Yes 19 No

Write a function to find the depth of a binary tree...

Answer / sai

int maxDepth(TreeNode *tree)
{
if (tree == NULL)
return 0;
else {
// compute the depth of each subtree
int lDepth = maxDepth(tree->left);
int rDepth = maxDepth(tree->right);

// use the larger one
if(lDepth > rDepth)
return (lDepth + 1);
else
return (rDepth + 1);
}
}

Is This Answer Correct ?    18 Yes 9 No

Write a function to find the depth of a binary tree...

Answer / ravi

Assuming we start with Level 1, (in Java)

{{{

public class BinaryTreeSearch {

/**
* @param args
*/
public static void main(String[] args) {

BinaryTreeSearch test = new BinaryTreeSearch();
Node root = test.new Node(null, null, "Value");
Node last = root;
Node middle = null;
for (int i = 0; i < 10; i++) {
Node temp = test.new Node(null, null, "Value");
last.setLeft(temp);
last = temp;
if (i == 5)
middle = temp;
}
for (int i = 0; i < 20; i++) {
Node temp = test.new Node(null, null, "Value");
middle.setRight(temp);
middle = temp;
}

for (int i = 0; i < 10; i++) {
Node temp = test.new Node(null, null, "Value");
last.setRight(temp);
last = temp;
}

int depth = depth(root, 0);
System.out.println(depth);
}

private static int depth(Node node, int level) {
if (null == node)
return level;
level++;
int leftLevel = depth(node.getLeft(), level);
int rightLevel = depth(node.getRight(), level);

if (leftLevel > rightLevel) {
return leftLevel;
} else {
return rightLevel;
}
}

public class Node {
Node left = null;
Node right = null;
String strValue = null;

public Node(Node left, Node right, String value) {
this.left = left;
this.right = right;
this.strValue = value;
}

public void setLeft(Node left) {
this.left = left;
}

public void setRight(Node right) {
this.right = right;
}

public void setValue(String value) {
this.strValue = value;
}

public Node getLeft() {
return left;
}

public Node getRight() {
return right;
}
}
}

}}}

Is This Answer Correct ?    10 Yes 5 No

Write a function to find the depth of a binary tree...

Answer / jimmyjj

// clean and simple
int maxDepth(Node *n) {
if (n == NULL) return 0;
int left = maxDepth(n->left);
int right = maxDepth(n->right);
return 1 + max(left, right);
}

Is This Answer Correct ?    8 Yes 3 No

Write a function to find the depth of a binary tree...

Answer / hithere

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 ?    13 Yes 12 No

Write a function to find the depth of a binary tree...

Answer / prakhar

//simple 2 liner
int depth(Node *n)
{
if (n==NULL) return 0;
return ( max( depth(n->left) , depth(n->right) ) + 1 )
}

Is This Answer Correct ?    1 Yes 0 No

Write a function to find the depth of a binary tree...

Answer / sanjeev sindhu

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 ?    13 Yes 14 No

Write a function to find the depth of a binary tree...

Answer / napster

#include<stdio.h> /* A tree node structure */struct node{
int data; struct node *left; struct node *right;}; /*
Helper function for getLevel(). It returns level of the
data if data is present in tree, otherwise returns
0.*/int getLevelUtil(struct node *node, int data, int level)
{ if ( node == NULL ) return 0; if ( node->data ==
data ) return level; return getLevelUtil ( node->left,
data, level+1) | getLevelUtil ( node->right, data,
level+1);} /* Returns level of given data value */int
getLevel(struct node *node, int data){ return getLevelUtil
(node,data,1);} /* Utility function to create a new Binary
Tree node */struct node* newNode(int data){ struct node
*temp = new struct node; temp->data = data; temp->left =
NULL; temp->right = NULL; return temp;} /* Driver
function to test above functions */int main(){ struct node
*root = new struct node; int x; /* Constructing tree
given in the above figure */ root = newNode(3); root-
>left = newNode(2); root->right = newNode(5); root->left-
>left = newNode(1); root->left->right = newNode(4); x =
3; printf(" Level of %d is %d", x, getLevel(root, x)); x
= 6; printf("\n Level of %d is %d", x, getLevel(root,
x)); getchar(); return 0;}

Is This Answer Correct ?    0 Yes 1 No

Post New Answer

More C Code Interview Questions

write a origram swaoing valu without 3rd variable

2 Answers  


void main() { while(1){ if(printf("%d",printf("%d"))) break; else continue; } }

1 Answers  


#define assert(cond) if(!(cond)) \ (fprintf(stderr, "assertion failed: %s, file %s, line %d \n",#cond,\ __FILE__,__LINE__), abort()) void main() { int i = 10; if(i==0) assert(i < 100); else printf("This statement becomes else for if in assert macro"); }

1 Answers  


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 Answers  


main() { struct date; struct student { char name[30]; struct date dob; }stud; struct date { int day,month,year; }; scanf("%s%d%d%d", stud.rollno, &student.dob.day, &student.dob.month, &student.dob.year); }

1 Answers  






#include<stdio.h> main() { struct xx { int x; struct yy { char s; struct xx *p; }; struct yy *q; }; }

1 Answers  


Write a C function to search a number in the given list of numbers. donot use printf and scanf

5 Answers   Honeywell, TCS,


main() { int i = 0xff ; printf("\n%d", i<<2); } a. 4 b. 512 c. 1020 d. 1024

2 Answers   HCL,


how to test pierrot divisor

0 Answers  


main() { unsigned char i=0; for(;i>=0;i++) ; printf("%d\n",i); }

1 Answers  


main() { int i=400,j=300; printf("%d..%d"); }

3 Answers  


main() { int i=0; for(;i++;printf("%d",i)) ; printf("%d",i); }

1 Answers   Zoho,


Categories