Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string.h>
- #include <fstream>
- #include <math.h>
- #include <queue>
- using namespace std;
- ifstream in("arbori.in");
- ofstream out("arbori.out");
- struct nod{
- int info;
- nod* st;
- nod* dr;
- };
- struct per{
- int nod;
- int inaltime;
- };
- per perchi[100];
- int index = 0;
- void creare(nod*& root);
- void sumPare(nod*& root);
- void Frunze(nod*& root);
- int inaltime(nod*& root);
- void noduriPeNivel(nod*& root, int k);
- nod *root;
- int s = 0;
- int main(){
- int a;
- creare(root);
- sumPare(root);
- out<<s;
- out<<endl;
- Frunze(root);
- out<<(inaltime(root)) - 1;
- out<<endl;
- int iMax = inaltime(root);
- noduriPeNivel(root, 0);
- int nivel = 0;
- for(int i = 0; i < index; i++){
- if(iMax < nivel - 1)
- break;
- out<<nivel<<" n: ";
- for(int j = 0; j < index; j++){
- if(nivel == perchi[j].inaltime){
- out<<perchi[j].nod<<" ";
- }
- }
- nivel++;
- out<<endl;
- }
- return 0;
- }
- void creare(nod*& root){
- int x;
- in>>x;
- if(x != 0){
- root = new nod;
- root->info = x;
- creare(root->st);
- creare(root->dr);
- } else {
- root = NULL;
- }
- }
- void sumPare(nod*& root){
- if(root == NULL){
- return;
- }
- if(root->info % 2 == 0){
- s+=root->info;
- }
- sumPare(root->st);
- sumPare(root->dr);
- }
- void Frunze(nod*& root){
- if(root == NULL){
- return;
- }
- if(root->st == NULL && root->dr == NULL){
- out<<root->info<<" ";
- return;
- }
- Frunze(root->st);
- Frunze(root->dr);
- }
- int inaltime(nod*& root){
- if(root == NULL){
- return 0;
- }
- return 1 + max(inaltime(root->st) , inaltime(root->dr));
- }
- void noduriPeNivel(nod*& root, int k){
- if(root == NULL){
- return;
- }
- per p;
- p.inaltime = k;
- p.nod = root->info;
- perchi[index++] = p;
- noduriPeNivel(root->st, k + 1);
- noduriPeNivel(root->dr, k + 1);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement