회원가입
로그인
Toggle navigation
문제
문제
전체 문제
문제 출처
단계별로 풀어보기
알고리즘 분류
추가된 문제
문제 순위
문제
푼 사람이 한 명인 문제
아무도 못 푼 문제
최근 제출된 문제
최근 풀린 문제
랜덤
출처
ICPC
Olympiad
한국정보올림피아드
한국정보올림피아드시․도지역본선
전국 대학생 프로그래밍 대회 동아리 연합
대학교 대회
카카오 코드 페스티벌
Coder's High
ICPC
Regionals
World Finals
Korea Regional
Africa and the Middle East Regionals
Europe Regionals
Latin America Regionals
North America Regionals
South Pacific Regionals
문제집
대회
채점 현황
게시판
그룹
더 보기
재채점 기록
블로그
도움말
전체
공지
자유
질문
오타/오역/요청
게시판 공지
홍보
업데이트
solved.ac
글쓰기
질문 도움말
자주묻는 질문
35% 시간초과
10989번 - 수 정렬하기 3
boringcoding
2년 전
0
트리로 입력하면서 노드만들고 count 세서 했는데 시간 초과가 납니다
#include<stdio.h> #include<string.h> #include<stdlib.h> struct node { int num; int count; struct node* l, * r; }; struct node* talloc(void) { return (struct node*)malloc(sizeof(struct node)); } struct node* addtree(struct node* p, int num) { int cond; if (p == NULL) { p = talloc(); p->num = num; p->count = 1; p->l = p->r = NULL; } else if ((cond = num - p->num) == 0) p->count++; else if (cond < 0) p->l = addtree(p->l, num); else p->r = addtree(p->r, num); return p; } void treeprint(struct node* p) { if (p == NULL) return; else { if (p->l != NULL) treeprint(p->l); for (int i = 0; i < p->count; i++) printf("%d\n", p->num); if (p->r != NULL) treeprint(p->r); } return; } int main(void) { int n; int line; struct node* root; root = NULL; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &line); root = addtree(root, line); } treeprint(root); return 0; }
zenith82114
2년 전
1
밸런싱 처리를 하지 않는 이진 검색 트리는 N번의 삽입/조회에 최대 O(N^2)의 시간을 소비합니다.
댓글을 작성하려면
로그인
해야 합니다.
boringcoding 2년 전
트리로 입력하면서 노드만들고 count 세서 했는데 시간 초과가 납니다