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

How can i find first 5 natural Numbers without using any loop in c language????????

2 Answers   Microsoft,


Is there any difference between the two declarations, 1. int foo(int *arr[]) and 2. int foo(int *arr[2])

1 Answers  


why the range of an unsigned integer is double almost than the signed integer.

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() { extern int i; { int i=20; { const volatile unsigned i=30; printf("%d",i); } printf("%d",i); } printf("%d",i); } int i;

1 Answers  






main() { 41printf("%p",main); }8

1 Answers  


write a program to Insert in a sorted list

4 Answers   Microsoft,


What is the hidden bug with the following statement? assert(val++ != 0);

1 Answers  


main() { int x=5; clrscr(); for(;x==0;x--) { printf("x=%d\n”", x--); } } a. 4, 3, 2, 1, 0 b. 1, 2, 3, 4, 5 c. 0, 1, 2, 3, 4 d. none of the above

3 Answers   HCL,


main() { int i; clrscr(); for(i=0;i<5;i++) { printf("%d\n", 1L << i); } } a. 5, 4, 3, 2, 1 b. 0, 1, 2, 3, 4 c. 0, 1, 2, 4, 8 d. 1, 2, 4, 8, 16

4 Answers   HCL,


main() { main(); }

1 Answers  


write a origram swaoing valu without 3rd variable

2 Answers  


Categories