Find the largest number in a binary tree

Answers were Sorted based on User's Feedback



Find the largest number in a binary tree..

Answer / guest

Well..it's not a binary search tree.So we need to traverse
entire binary tree and check with the all node elements and
find the max value.
struct node
{
int data;
struct node *l;
struct node *r;
};
typedef struct node *nd;
int maximum(nd root)
{
static int max;
nd cur = root;
if(cur!=NULL)
{
if(cur->data>max)
max=cur->data;
maximum(root->l);
maximum(root->r);
}
return max;
}

Is This Answer Correct ?    13 Yes 11 No

Find the largest number in a binary tree..

Answer / tomás senart

There is a difference between a binary tree and a binary search tree.
A binary tree isn't organized in any sense. The values of it's nodes can be random and have no relationship to each other.

Here is a method for finding the biggest node on a binary tree.

typedef struct node {
int value;
struct tree *right;
struct tree *left;
} Node;

int biggest_node(Node *node)
{
int biggest_left, biggest_right;

biggest_left = node->left ? biggest_node(node->left) : node->value;
biggest_right = node->right ? biggest_node(node->right) : node->value;

if(node->value < biggest_left && node->value < biggest_right)
return biggest_left > biggest_right ? biggest_left : biggest_right;
else if(node->value < biggest_right)
return biggest_right;
else if(node->value < biggest_left)
return biggest_left;
else
return node->value;
}

Is This Answer Correct ?    7 Yes 6 No

Find the largest number in a binary tree..

Answer / jiabul sk

int maxOfTree(tree * t)
{
//if tree is empty then it will return -99
if(t==null){return -99;}
else
{
int temp ;
temp=max( maxOfTree(t->left), maxOfTree(t->right));
return( max( t->info , temp ) );

}





}

Is This Answer Correct ?    4 Yes 3 No

Find the largest number in a binary tree..

Answer / c++ genie

<code>
struct node
{
int data;
struct node *l;
struct node *r;
};
typedef struct node *nd;
int maximum(nd root)
{
static int max;
nd cur = root;
if(cur!=NULL)
{
int temp = maximum(root->l);
if(temp > max)
max = temp;
int temp2 = maximum(root->r);
if(temp2 > max)
max = temp;
}
return max;
}
</code>

Is This Answer Correct ?    2 Yes 4 No

Find the largest number in a binary tree..

Answer / raghuram.a

Well..it's not a binary search tree.So we need to traverse
entire binary tree and check with the all node elements and
find the max value.
struct node
{
int data;
struct node *l;
struct node *r;
};
typedef struct node *nd;
int maximum(nd root)
{
static int max;
nd cur = root;
if(cur!=NULL)
{
if(cur->data>max)
max=cur->data;
maximum(root->l);
maximum(root->r);
}
return max;
}

Is This Answer Correct ?    4 Yes 10 No

Find the largest number in a binary tree..

Answer / guest

3333598209374158490386452138699.3

Is This Answer Correct ?    4 Yes 12 No

Find the largest number in a binary tree..

Answer / bharat pandey

The Largest Node in the Binary tree is the Rightmost node
of the tree.
Hence we would traverse the Tree Till The Rightmost child
of the node is traversed.

the code is as follows:


#include<stdio.h>
#include<conio.h>

struct node
{
int data;
struct node *left,*right;
}*tree;

struct node* MAX(struct node* q)
{
struct node* temp;
while(q->right!=NULL)
{
temp=q;
q=q->right;
}
return temp;
}


this algorithm will find the largest element of the tree in
o(log n).

Is This Answer Correct ?    6 Yes 23 No

Post New Answer

More C Code Interview Questions

main() { extern int i; i=20; printf("%d",sizeof(i)); }

2 Answers  


#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  


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() { int i=5; printf(ā€œ%dā€,i=++i ==6); }

1 Answers  


1) int i=5; j=i++ + i++ + i++; printf("%d",j);This code gives the answer 15.But if we replace the value of the j then anser is different?why? 2)int i=5; printf("%d",i++ + i++ + i++); this givs 18.

8 Answers   IBPS, Infosys, TCS,






main() { int i=-1; -i; printf("i = %d, -i = %d \n",i,-i); }

1 Answers  


There are 21 people in a room. They have to form groups of 3 people each. How many combinations are possible? Write a C program to print the same.

1 Answers   TCS,


program to find the roots of a quadratic equation

14 Answers   College School Exams Tests, Engineering, HP, IIIT, Infosys, Rajiv Gandhi University of Knowledge Technologies RGUKT, SSC,


Write out a function that prints out all the permutations of a string. For example, abc would give you abc, acb, bac, bca, cab, cba. You can assume that all the characters will be unique.

5 Answers   IITR, Microsoft, Nike,


main() { int i = 257; int *iPtr = &i; printf("%d %d", *((char*)iPtr), *((char*)iPtr+1) ); }

1 Answers   CSC,


Hi, i have a project that the teacher want a pyramid of numbers in C# or java...when we click a button...the pyramid should be generated in a listbox/or JtextArea...and the pyramid should have the folowing form: 1 232 34543 4567654 567898765 67890109876 7890123210987 890123454321098 90123456765432109 0123456789876543210 Plz help with codes...didn't find anything on the net.

0 Answers  


What is the main difference between STRUCTURE and UNION?

13 Answers   HCL,


Categories