#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct arvore {
int key;
struct arvore * right, *left;
};
typedef struct arvore Arvore;
Arvore * alocar(int key) {
Arvore
* p
= (Arvore
*)malloc(sizeof(Arvore
)); p->key = key;
p->right = NULL;
p->left = NULL;
return p;
}
int procurafolha(Arvore * p) {
int a = 0;
if(p->right == NULL && p->left == NULL) {
return p->key;
} else {
if(p->left != NULL) {
a = a + procurafolha(p->left);
}
if(p->right != NULL) {
a = a + procurafolha(p->right);
}
}
return a;
}
int procurasemfolha(Arvore * p) {
int a = 0;
if(p->left != NULL) {
a = a + procurasemfolha(p->left);
}
if(p->right != NULL) {
a = a + procurasemfolha(p->right);
}
if(p->right != NULL || p->left != NULL) {
a = a + p->key;
}
return a;
}
Arvore * insere(Arvore * p, int key) {
if(p == NULL) {
p = alocar(key);
return p;
} else if(key < p->key) {
p->left = insere(p->left, key);
} else if(key > p->key) {
p->right = insere(p->right, key);
}
return p;
}
void libera(Arvore * p) {
if(p != NULL) {
libera(p->left);
libera(p->right);
p = NULL;
}
}
int main() {
Arvore *p = NULL;
char a;
int n, i = 0;
int qtd, qtd01;
while(1) {
p = insere(p, n);
if(a == '\n') {
break;
}
}
qtd = procurafolha(p);
qtd01 = procurasemfolha(p);
printf("\n\n%d %d\n", qtd
, qtd01
);
libera(p);
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KI2luY2x1ZGUgPHN0cmluZy5oPgoKCnN0cnVjdCBhcnZvcmUgewoJaW50IGtleTsKCXN0cnVjdCBhcnZvcmUgKiByaWdodCwgKmxlZnQ7CgoKfTsKCnR5cGVkZWYgc3RydWN0IGFydm9yZSBBcnZvcmU7CgpBcnZvcmUgKiBhbG9jYXIoaW50IGtleSkgewoJQXJ2b3JlICogcCA9IChBcnZvcmUgKiltYWxsb2Moc2l6ZW9mKEFydm9yZSkpOwoJcC0+a2V5ID0ga2V5OwoJcC0+cmlnaHQgPSBOVUxMOwo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