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

main() { { unsigned int bit=256; printf("%d", bit); } { unsigned int bit=512; printf("%d", bit); } } a. 256, 256 b. 512, 512 c. 256, 512 d. Compile error

1 Answers   HCL,


#include <stdio.h> #define a 10 main() { #define a 50 printf("%d",a); }

2 Answers  


pls anyone can help me to write a code to print the values in words for any value.Example:1034 to print as "one thousand and thirty four only"

2 Answers  


main() { main(); }

1 Answers  


How do you verify if the two sentences/phrases input is an anagram using predefined functions in string.h and by using arrays?

0 Answers  






Given a list of numbers ( fixed list) Now given any other list, how can you efficiently find out if there is any element in the second list that is an element of the first list (fixed list)

3 Answers   Disney, Google, ZS Associates,


#include <stdio.h> main() { char * str = "hello"; char * ptr = str; char least = 127; while (*ptr++) least = (*ptr<least ) ?*ptr :least; printf("%d",least); }

1 Answers  


WAP to display 1,2,3,4,5........N

2 Answers  


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

2 Answers   HCL,


void func1(int (*a)[10]) { printf("Ok it works"); } void func2(int a[][10]) { printf("Will this work?"); } main() { int a[10][10]; func1(a); func2(a); } a. Ok it works b. Will this work? c. Ok it worksWill this work? d. None of the above

1 Answers   HCL,


how to check whether a linked list is circular.

11 Answers   Microsoft,


Is the following code legal? struct a { int x; struct a *b; }

2 Answers  


Categories