Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- ifstream in("date.in");
- struct node {
- int inf;
- node *st, *dr;
- };
- node *prim = new node;
- bool ok;
- node *creare() {
- int x;
- in >> x;
- if ( x == 0 )
- return NULL;
- else {
- node *nou;
- nou = new node;
- nou->inf = x;
- if(!ok)
- prim = nou;
- ok = 1;
- nou->st = creare();
- nou->dr = creare();
- return nou;
- }
- }
- int maxim ( node *p ) {
- if ( p != NULL ) {
- int m = max ( maxim ( p->st ), maxim ( p->dr ) );
- if ( m == 0 )
- m = p->inf;
- return m;
- }
- return 0;
- }
- int suma ( node *p ) {
- if ( p != NULL ) {
- int s1, s2;
- s1 = suma ( p->st );
- s2 = suma ( p->dr );
- return s1 + s2 + p->inf;
- } else
- return 0;
- }
- void nivel(node*p,int k,int niv){
- if(p!=NULL){
- if(k==niv)
- {
- cout<<p->inf<<" ";
- return;
- }
- nivel(p->st,k+1,niv);
- nivel(p->dr,k+1,niv);
- }
- }
- int main() {
- creare();
- cout<<maxim(prim)<<'\n';
- cout<<suma(prim)<<'\n';
- int niv;
- cin>>niv;
- nivel(prim,1,niv);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement