5
A binary search tree is strictly binary if all tree nodes have 2 children or none. Implement a function that checks whether a binary search tree is strictly binary.
// Estrutura de dados
typedef struct {
int chave;
}tipo_elemento;
typedef struct nodo{
struct nodo *dir, *esq;
tipo_elemento e;
}tipo_nodo;
typedef tipo_nodo *apontador;
// Implementação
int estritamente_bin(apontador a){
if(!a->dir && !a->esq)
return 1;
if(a->dir && a->esq)
return estritamente_bin(a->esq) && estritamente_bin(a->dir);
return 0;
}
Any suggestions and/or criticism to improve the above implementation?
Your code is correct. If tree change operations are few, but state query operations are many, it is possible to insert some metadata into tree nodes to contain this information and be more efficient. I’m not sure either
code review
is appropriate to the Sopt model, so vote for closure– Jefferson Quesado