Skip to content
29 changes: 29 additions & 0 deletions solutions/yujin/0130/[문제28]트리순회.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const fs = require('fs');
const input = fs.readFileSync('/dev/stdin').toString().trim().split('\n');

const N = Number(input[0]);
const tree = {};

for (let i = 1; i <= N; i++) {
const [node, left, right] = input[i];
tree[node] = { left, right };
}

function preorder(node) {
if (node === '.') return '';
return node + preorder(tree[node].left) + preorder(tree[node].right);
}

function inorder(node) {
if (node === '.') return '';
return preorder(tree[node].left) + node + preorder(tree[node].right);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

inorder 호출해야하는데 preorder를 호출하고 있는 것 같아요..!

}

function postorder(node) {
if (node === '.') return '';
return postorder(tree[node].left) + postorder(tree[node].right) + node;
}

console.log(preorder('A'));
console.log(inorder('A'));
console.log(postorder('A'));
61 changes: 61 additions & 0 deletions solutions/yujin/0130/[문제29]이진탐색트리구현.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
class Node {
constructor(value) {
this.left = null;
this.right = null;
this.value = value;
}
}

class BST {
constructor() {
this.root = null;
}
insert(value) {
if (!this.root) {
this.root = new Node(value);
return;
} else {
let cur = this.root;
while (true) {
// 삽입 하려는 값이 현재 자식노드보다 작은 경우
if (value < cur) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if(value < cur.value) 이렇게 되어야 할 것 같아요!

// 왼쪽으로 이동
if (cur.left) {
cur = cur.left;
} else {
cur.left = new Node(value);
break;
}
} else {
if (cur.right) {
cur = cur.right;
} else {
cur.right = new Node(value);
break;
}
}
}
}
}

search(value) {
let cur = this.root;
while (cur) {
if (cur.value === value) return true;
else if (cur.value < value) cur = cur.left;
else cur = cur.right;
}
return false;
}
}

function solution(lst, searchList) {
const bst = new BST();

for (const num of lst) {
bst.insert(num);
}
return searchList.map((v) => bst.search(v));
}

console.log(solution([5, 3, 8, 4, 2, 1, 7, 10], [1, 2, 5, 6]));
11 changes: 11 additions & 0 deletions solutions/yujin/0130/[문제30]예상대진표.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function solution(n, a, b) {
let answer = 0;

while (a !== b) {
a = Math.ceil(a / 2);
b = Math.ceil(b / 2);
answer++;
}

return answer;
}
48 changes: 48 additions & 0 deletions solutions/yujin/0130/트리_순회.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
function solution(arr) {
// 전위 순회
function preOrder(nodes, idx) {
const left = idx * 2 + 1;
const right = idx * 2 + 2;

if (idx < nodes.length) {
let ret = `${nodes[idx]}`;
ret += preOrder(nodes, left);
ret += preOrder(nodes, right);

return ret;
}
return '';
}

// 중위 순회
function inOrder(nodes, idx) {
const left = idx * 2 + 1;
const right = idx * 2 + 2;

if (idx < nodes.length) {
let ret = inOrder(nodes, left);
ret += `${nodes[idx]}`;
ret += inOrder(nodes, right);
return ret;
}
return '';
}

// 후위 순회
function postOrder(nodes, idx) {
let left = idx * 2 + 1;
let right = idx * 2 + 2;

if (idx < nodes.length) {
let ret = postOrder(nodes, left);
ret += postOrder(nodes, right);
ret += `${nodes[idx]}`;
return ret;
}
return '';
}

return [preOrder(arr, 0), inOrder(arr, 0), postOrder(arr, 0)];
}

console.log(solution([1, 2, 3, 4, 5, 6, 7]));