diff --git "a/solutions/yujin/0130/[\353\254\270\354\240\23428]\355\212\270\353\246\254\354\210\234\355\232\214.js" "b/solutions/yujin/0130/[\353\254\270\354\240\23428]\355\212\270\353\246\254\354\210\234\355\232\214.js" new file mode 100644 index 0000000..5430df1 --- /dev/null +++ "b/solutions/yujin/0130/[\353\254\270\354\240\23428]\355\212\270\353\246\254\354\210\234\355\232\214.js" @@ -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); +} + +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')); diff --git "a/solutions/yujin/0130/[\353\254\270\354\240\23429]\354\235\264\354\247\204\355\203\220\354\203\211\355\212\270\353\246\254\352\265\254\355\230\204.js" "b/solutions/yujin/0130/[\353\254\270\354\240\23429]\354\235\264\354\247\204\355\203\220\354\203\211\355\212\270\353\246\254\352\265\254\355\230\204.js" new file mode 100644 index 0000000..dbd0967 --- /dev/null +++ "b/solutions/yujin/0130/[\353\254\270\354\240\23429]\354\235\264\354\247\204\355\203\220\354\203\211\355\212\270\353\246\254\352\265\254\355\230\204.js" @@ -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) { + // 왼쪽으로 이동 + 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])); diff --git "a/solutions/yujin/0130/[\353\254\270\354\240\23430]\354\230\210\354\203\201\353\214\200\354\247\204\355\221\234.js" "b/solutions/yujin/0130/[\353\254\270\354\240\23430]\354\230\210\354\203\201\353\214\200\354\247\204\355\221\234.js" new file mode 100644 index 0000000..b05f9bc --- /dev/null +++ "b/solutions/yujin/0130/[\353\254\270\354\240\23430]\354\230\210\354\203\201\353\214\200\354\247\204\355\221\234.js" @@ -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; +} diff --git "a/solutions/yujin/0130/\355\212\270\353\246\254_\354\210\234\355\232\214.js" "b/solutions/yujin/0130/\355\212\270\353\246\254_\354\210\234\355\232\214.js" new file mode 100644 index 0000000..d4b099e --- /dev/null +++ "b/solutions/yujin/0130/\355\212\270\353\246\254_\354\210\234\355\232\214.js" @@ -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]));