티스토리 뷰
https://www.acmicpc.net/problem/5639
class Node{
constructor(data,left,right){
this.data = data;
this.left = left;
this.right = right;
}
show(){
return this.data;
}
}
class BST{
constructor(){
this.root = null;
this.preOrderList = '';
this.inOrderList = '';
this.postOrderList = '';
}
insert(data){
let n = new Node(data,null,null);
if(this.root == null){
this.root = n;
}else{
let current = this.root;
let parent;
while(true){
parent = current;
if(data<current.data){
current = current.left;
if(current==null){
parent.left = n;
break;
}
}else{
current = current.right;
if(current == null){
parent.right = n;
break;
}
}
}
}
}
}
const fs = require('fs');
const input = fs.readFileSync("./dev/stdin").toString().trim().split("\n").map(Number);
const tree = new BST();
input.forEach(v=>{
tree.insert(v);
})
let postOrderList = [];
function postOrder(node){
if((node != null)){
postOrder(node.left);
postOrder(node.right);
postOrderList.push(node.data);
}
}
postOrder(tree.root);
console.log(postOrderList.join('\n'))
728x90
'자료구조 알고리즘 > 백준' 카테고리의 다른 글
Node.js) 백준 1476번: 날짜계산 (0) | 2021.12.13 |
---|---|
Node.js)백준 1956번: 운동 (0) | 2021.09.28 |
Node.js) 백준 1991번: 트리 순회 (0) | 2021.09.27 |
Node.js) 백준 11725번: 트리의 부모 찾기 (0) | 2021.09.27 |
Node.js) 백준 11404번: 플로이드 (0) | 2021.09.26 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 면접질문
- 서버개발
- 서버점검
- 투포인터 연습
- MOD
- 그래프
- 개발자면접
- 최소공통조상
- create databases;
- node.js
- 투포인터
- DB 생성
- 동적프로그래밍
- create db
- 로드나인
- 다이나밍프로그래밍
- MySQL
- 면접비
- 롱베케이션
- 다이나믹프로그래밍
- BFS
- 은둔청년체험
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
글 보관함