1
I’m trying to solve URI Online Judge Issue 1463, link: https://www.urionlinejudge.com.br/judge/pt/problems/view/1463 However I get the message 100% error or time limit exceeded, someone could show me what would be wrong in the code below ?
#include <stdio.h>
#include <stdlib.h>
typedef struct node{
char letra;
struct node *esq;
struct node *dir;
}Arvore;
Arvore* Insere(Arvore *raiz,char letra){
if(raiz == NULL){
Arvore *nova = (Arvore*) malloc(sizeof(Arvore));
nova->dir = NULL;
nova->esq = NULL;
nova->letra = letra;
return nova;
}
else if(raiz->esq == NULL){
char temp = raiz->letra;
raiz->letra = letra;
raiz->esq = Insere(raiz->esq,temp);
}
else if(raiz->dir == NULL){
raiz->dir = Insere(raiz->dir,letra);
}
else{
Arvore *nova_raiz = (Arvore*) malloc(sizeof(Arvore));
nova_raiz->dir = NULL;
nova_raiz->esq = raiz;
nova_raiz->letra = letra;
return nova_raiz;
}
}
printGivenLevel(Arvore* node, int level){
if (node == NULL)
return;
if (level == 1)
printf(" %c", node->letra);
else if (level > 1)
{
printGivenLevel(node->esq, level-1);
printGivenLevel(node->dir, level-1);
}
}
int altura (Arvore *node) {
if (node == NULL)
return -1;
else {
int Altura_esq = altura (node->esq);
int Altura_dir = altura (node->dir);
if (Altura_esq < Altura_dir){
return Altura_dir + 1;
}
else{
return Altura_esq + 1;
}
}
}
int main(){
Arvore *raiz = NULL;
int i,altura_arv;
char letra,letra2;
while(scanf("%c",&letra)){
fflush(stdin);
if(letra == ' ' || letra == '\n'){
continue;
}
else if(letra != '('){
//printf("Entrou\n");
raiz = Insere(raiz,letra);
}
else{
//printf("Entrou2\n");
scanf("%c",&letra2);
fflush(stdin);
raiz->dir = Insere(raiz->dir,letra2);
}
if(letra == EOF){
altura_arv = altura(raiz);
for(i=0;i<altura_arv;i++){
printf("Nivel %d:",i);
printGivenLevel(raiz,i+1);
printf("\n");
}
raiz = NULL;
}
}
}
Usually TLE is a sign of: a) you have entered an infinite loop unintentionally; or b) your algorithm is inefficient for one or more test inputs. Try doing a program that generates valid entries and try running your program against those automatically generated entries. Then check where the slowness is. And yes, as a creator of competitive programming issues, I would create an input-generating program to determine what the expected time frame of the solution would be for whoever submitted the question. Usually 10 or 100 times slower than my solution
– Jefferson Quesado
It seems to me that you are inserting into the tree according to the input order and not according to the operators/operands precedence.
– anonimo