Skip to content

Commit

Permalink
Feat: graph, recursion(이진 검색 트리)
Browse files Browse the repository at this point in the history
  • Loading branch information
luckylooky2 committed May 10, 2024
1 parent 13be1ce commit 32a5c73
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions baekjoon/5639.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// 이진 검색 트리 : 그래프 탐색, 재귀 호출
const input = require("fs")
.readFileSync("/dev/stdin")
.toString()
.trim()
.split("\n");
const arr = input.map((v) => Number(v));
let answer = [];

function recur(arr) {
if (arr.length <= 1) {
if (arr.length) {
answer.push(arr[0]);
}
return;
}

let root, left, right;
root = [arr[0]];
right = [];
left = arr.slice(1);
for (let i = 1; i < arr.length; i++) {
if (arr[i] > root[0]) {
right = arr.slice(i);
left = arr.slice(1, i);
break;
}
}
recur(left);
recur(right);
recur(root);
}

recur(arr);

console.log(answer.length ? answer.join("\n") : arr[0]);

0 comments on commit 32a5c73

Please sign in to comment.