-
Notifications
You must be signed in to change notification settings - Fork 3
[이유진] 01.30 트리 문제 제출 #27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
JIN921
wants to merge
13
commits into
KNY1005:main
Choose a base branch
from
JIN921:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
274f8f5
solve: 스택 > 문제 8 ~ 문제 12
JIN921 9e21cc4
solve: 문제 13
JIN921 4dc6e14
Merge branch 'KNY1005:main' into main
JIN921 674e7d8
solve : 문제 15 ~ 17번 정답 및 큐 구현
JIN921 9134d6b
Merge branch 'KNY1005:main' into main
JIN921 3b9bad0
Merge branch 'KNY1005:main' into main
JIN921 6b6771c
solve : 문제 18 ~ 22번 풀이
JIN921 81e05b5
solve : 문제 23번 풀이
JIN921 9ce22da
Merge branch 'KNY1005:main' into main
JIN921 9f13a95
solve : 문제 24 ~ 27번 풀이
JIN921 4e185dd
Merge branch 'KNY1005:main' into main
JIN921 61d322a
solve : 문제 28 ~ 30번 풀이
JIN921 bb242df
Merge branch 'KNY1005:main' into main
JIN921 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
|
|
||
| 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')); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| // 왼쪽으로 이동 | ||
| 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])); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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])); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
inorder호출해야하는데preorder를 호출하고 있는 것 같아요..!