#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;
}
}
void mostrar(Arvore *p){
if (p == NULL) return;
mostrar(p->left);
mostrar(p->right);
}
int soma_folhas(Arvore *p){
if (p == NULL) return 0;
int valorNo = (p->left == NULL && p->right == NULL) ? p->key : 0;
return valorNo + soma_folhas(p->left) + soma_folhas(p->right);
}
int soma_nao_folhas(Arvore *p){
if (p == NULL) return 0;
int valorNo = (p->left == NULL && p->right == NULL) ? 0 : p->key;
return valorNo + soma_nao_folhas(p->left) + soma_nao_folhas(p->right);
}
int main() {
Arvore *p = NULL;
char a;
int n, i = 0;
int qtd, qtd01;
while(1) {
p = insere(p, n);
if(a == '\n') {
break;
}
}
//mostrar(p);
qtd = soma_folhas(p);
qtd01 = soma_nao_folhas(p);
printf("\n\n%d %d\n", qtd
, qtd01
);
libera(p);
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KI2luY2x1ZGUgPHN0cmluZy5oPgoKCn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