Cod sursa(job #2145836)

Utilizator victorv88Veltan Victor victorv88 Data 27 februarie 2018 17:23:59
Problema Subsir crescator maximal Scor 5
Compilator cpp Status done
Runda Arhiva educationala Marime 1.03 kb
#include <iostream>
#include <fstream>

using namespace std;

ifstream f("scmax.in");
ofstream g("scmax.out");

int n, a[100005], sol[100005], maxim,nrmaxi;

struct node
{
    int val;
    node *left, *right;
}*root;

void add(int x, node *&root)
{
    if (!root)
    {
        root=new node;
        root->val=x;
        root->left=0;
        root->right=0;
        return;
    }
    if (x<=root->val)
    {
        add(x,root->left);
    }
    else
        add(x,root->right);
}

/*void in(node *root)
{
    if (!root)
        return;
    in(root->left);
    printf("%d ",root->val);
    in(root->right);
}*/

void parcurgere(node *root,int nr)
{
    if (!root)
    {
        if (nr>nrmaxi)
            nrmaxi=nr;
        return;
    }
    parcurgere(root->right,nr+1);
    parcurgere(root->left,0);
}

int main()
{
    f >> n;
    root=0;
    for (int i=0; i<n; i++)
    {
        f >> a[i];
        add(a[i],root);
    }
    parcurgere(root,0);
    g << nrmaxi;
    return 0;
}