Answers for "maximum element in a binary tree"

C++
0

maximum element in a binary tree

#include<iostream>
using namespace std;
struct node
{
    int data;
    node* left;
    node* right;
};
node* getnode(int value)
{
    node* temp=new node;
    temp->data=value;
    temp->left=NULL;
    temp->right=NULL;
    return temp;
}
node* insert_bst(node* roots,int value)
{
    if(roots==NULL)
    {
        return getnode(value);
    }
    if(roots->data>value)
    {
        roots->left=insert_bst(roots->left,value);
    }
    else if(roots->data<value)
    {
        roots->right=insert_bst(roots->right,value);
    }
   return roots;
}
int max_element_bst(node* roots)
{

    if(roots==NULL)
    {
        return -1;
    }
    if(roots->right==NULL)
    {
        return roots->data;
    }
    else
    {
        return max_element_bst(roots->right);
    }
}
int main()
{
    node* root=new node;
    root=NULL;
    int value;
    do
    {
        cin>>value;
        if(value>0)
        {
            root=insert_bst(root,value);
        }
    }while(value>0);
    cout<<"Maximum element is "<<max_element_bst(root)<<endl;
}
Posted by: Guest on August-01-2021

Code answers related to "maximum element in a binary tree"

Browse Popular Code Answers by Language