Cod sursa(job #2454890)

Utilizator Catalin2002Catalin Craciun Catalin2002 Data 10 septembrie 2019 09:37:26
Problema Subsir crescator maximal Scor 5
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.25 kb
#include <iostream>
#include <fstream>
using namespace std;

ifstream fin("scmax.in");
ofstream fout("scmax.out");

int max2=1;

struct nod{

int info;

nod *stanga;
nod *dreapta;

}radacina;

void functie(nod &nod1, int nr,int max1)
{
    if(nod1.info>nr)
    {
        if(nod1.stanga!=NULL)
        {
            functie(*(nod1.stanga),nr,max1);
        }
        else
        {
             nod1.stanga=new nod;
            (*nod1.stanga).info=nr;
            (*nod1.stanga).stanga=NULL;
            (*nod1.stanga).dreapta=NULL;

        }

    }

    if(nod1.info<nr)
    {
        if(nod1.dreapta!=NULL)
            functie(*nod1.dreapta,nr,max1+1);
        else
        {
            nod1.dreapta=new nod;
            (*nod1.dreapta).info=nr;
            (*nod1.dreapta).dreapta=NULL;
            (*nod1.dreapta).stanga=NULL;

            if(max1>max2)
                max2=max1;
        }

        if(nod1.stanga!=NULL)
        {
            functie(*(nod1.stanga),nr,max1);
        }


    }


}

int main()
{
    int i,n,x;

    fin>>n;

    fin>>radacina.info;

    for(i=2;i<=n;i++)
    {
           fin>>x;

           functie(radacina,x,1);

    }

    fout<<max2+1;



    return 0;
}