2751번 - 수 정렬하기 2
import java.util.*;
public class HelloWorld{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int testcaseNum = sc.nextInt();
node root = new node(sc.nextInt());
for(int i =1; i < testcaseNum; i++){
int num = sc.nextInt();
node a = new node(num);
root.insertBst(a);
}
root.print();
class node{
int key;
node rightchild;
node leftchild;
node(int key){
this.key = key;
void insertBst(node a){
if(this.key > a.key){
if(this.leftchild == null){
this.leftchild = a;
}else{
this.leftchild.insertBst(a);
if(this.rightchild == null){
this.rightchild = a;
this.rightchild.insertBst(a);
void print(){
if(this.leftchild != null){
this.leftchild.print();
System.out.println(this.key);
if(this.rightchild != null){
this.rightchild.print();
};
생각해보니 소트하는건데 bst를 왜 썼나 싶은데
그래서 시간초과가 뜬 걸까요? 일반 소트보단 빠르지 않으려나요
댓글을 작성하려면 로그인해야 합니다.
lkm9709 3년 전
import java.util.*;
public class HelloWorld{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int testcaseNum = sc.nextInt();
node root = new node(sc.nextInt());
for(int i =1; i < testcaseNum; i++){
int num = sc.nextInt();
node a = new node(num);
root.insertBst(a);
}
root.print();
}
}
class node{
int key;
node rightchild;
node leftchild;
node(int key){
this.key = key;
}
void insertBst(node a){
if(this.key > a.key){
if(this.leftchild == null){
this.leftchild = a;
}else{
this.leftchild.insertBst(a);
}
}else{
if(this.rightchild == null){
this.rightchild = a;
}else{
this.rightchild.insertBst(a);
}
}
}
void print(){
if(this.leftchild != null){
this.leftchild.print();
}
System.out.println(this.key);
if(this.rightchild != null){
this.rightchild.print();
}
}
};
생각해보니 소트하는건데 bst를 왜 썼나 싶은데
그래서 시간초과가 뜬 걸까요? 일반 소트보단 빠르지 않으려나요