Skip to content

Commit

Permalink
24-08-05
Browse files Browse the repository at this point in the history
  • Loading branch information
pu2rile committed Aug 5, 2024
1 parent 5538bc9 commit ddd4aec
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 1 deletion.
Binary file modified .DS_Store
Binary file not shown.
Binary file modified pu2rile/.DS_Store
Binary file not shown.
6 changes: 5 additions & 1 deletion pu2rile/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@
| 7์ฐจ์‹œ | 2024.05.10 | ์™„์ „ ํƒ์ƒ‰ ์•Œ๊ณ ๋ฆฌ์ฆ˜ | [์˜ํ™”๊ฐ๋… ์ˆŒ](https://www.acmicpc.net/problem/1436) | [#26](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/25#issue-2289086909)
| 8์ฐจ์‹œ | 2024.05.14 | ๊ทธ๋ฆฌ๋”” ์•Œ๊ณ ๋ฆฌ์ฆ˜ | [ํŒ”](https://www.acmicpc.net/problem/1105) | [#28](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/28#issue-2295901384)
| 9์ฐจ์‹œ | 2024.05.27 | ๊ตฌํ˜„ | [์˜ค๋Š˜๋„ ์กŒ๋‹ค](https://www.acmicpc.net/problem/14582) | [#29](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/29#issue-2320060288)
| 10์ฐจ์‹œ | 2024.07.11 | ์Šคํƒ | [ํ™”ํ•™์‹๋Ÿ‰](https://www.acmicpc.net/problem/2257) | [#35](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/35#issue-2403173169)
| 10์ฐจ์‹œ | 2024.07.11 | ์Šคํƒ | [ํ™”ํ•™์‹๋Ÿ‰](https://www.acmicpc.net/problem/2257) | [#35](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/35#issue-2403173169)
| 11์ฐจ์‹œ | 2024.07.13 | ์šฐ์„ ์ˆœ์œ„ ํ | [๊ฐ•์˜์‹ค](https://www.acmicpc.net/problem/1374) | [#37](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/37#issue-2406937336)
| 12์ฐจ์‹œ | 2024.07.23 | ๋™์  ํ”„๋กœ๊ทธ๋ž˜๋ฐ | [1ํ•™๋…„](https://www.acmicpc.net/problem/5557) | [#40](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/40)
| 13์ฐจ์‹œ | 2024.07.26 | ์Šคํƒ | [ํ›„์œ„ ํ‘œ๊ธฐ์‹](https://www.acmicpc.net/problem/1918) | [#43](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/43)
| 14์ฐจ์‹œ | 2024.08.05 | ํŠธ๋ฆฌ | [์ด์ง„ ํƒ์ƒ‰ ํŠธ๋ฆฌ](https://www.acmicpc.net/problem/5639) | [#45](https://github.com/AlgoLeadMe/AlgoLeadMe-10/pull/45)
Binary file added pu2rile/ํŠธ๋ฆฌ/.DS_Store
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

typedef struct TreeNode {
int data;
struct TreeNode *left, *right;
} TreeNode;

// ์ƒˆ๋กœ์šด ๋…ธ๋“œ๋ฅผ ์ƒ์„ฑํ•˜๋Š” ํ•จ์ˆ˜
TreeNode* new_node(int key) {
TreeNode* node = (TreeNode*)malloc(sizeof(TreeNode));
node->data = key;
node->left = node->right = NULL;
return node;
}

// ์ „์œ„ ์ˆœํšŒ ๋ฐฐ์—ด์„ ์ด์šฉํ•ด BST๋ฅผ ์ƒ์„ฑํ•˜๋Š” ํ•จ์ˆ˜
TreeNode* construct_bst(int pre[], int* preIndex, int key, int min, int max, int size) {
// ํŠธ๋ฆฌ๊ฐ€ ๊ณต๋ฐฑ์ด๋ฉด NULL ๋ฐ˜ํ™˜
if (*preIndex >= size)
return NULL;

TreeNode* root = NULL;

// ํ˜„์žฌ key๊ฐ€ min๊ณผ max ์‚ฌ์ด์— ์žˆ์„ ๋•Œ๋งŒ ๋…ธ๋“œ๋ฅผ ์ƒ์„ฑ
if (key > min && key < max) {
root = new_node(key);
*preIndex = *preIndex + 1;

if (*preIndex < size) {
// ์™ผ์ชฝ ์„œ๋ธŒํŠธ๋ฆฌ๋ฅผ ๊ตฌ์„ฑ
root->left = construct_bst(pre, preIndex, pre[*preIndex], min, key, size);
}
if (*preIndex < size) {
// ์˜ค๋ฅธ์ชฝ ์„œ๋ธŒํŠธ๋ฆฌ๋ฅผ ๊ตฌ์„ฑ
root->right = construct_bst(pre, preIndex, pre[*preIndex], key, max, size);
}
}
return root;
}

// ํ›„์œ„ ์ˆœํšŒ ํ•จ์ˆ˜
void post_order(TreeNode* root) {
if (root) {
post_order(root->left); // ์™ผ์ชฝ ์„œ๋ธŒ ํŠธ๋ฆฌ ์ˆœํšŒ
post_order(root->right); // ์˜ค๋ฅธ์ชฝ ์„œ๋ธŒ ํŠธ๋ฆฌ ์ˆœํšŒ
printf("%d\n", root->data); // ๋…ธ๋“œ ๋ฐฉ๋ฌธ
}
}

int main(void) {
int pre[10000]; // ์ตœ๋Œ€ 10000๊ฐœ์˜ ๋…ธ๋“œ๋ฅผ ์ž…๋ ฅ๋ฐ›์„ ์ˆ˜ ์žˆ๋„๋ก ๋ฐฐ์—ด ํฌ๊ธฐ๋ฅผ ์ง€์ •
int key;
int size = 0;

while (scanf("%d", &key) != EOF) {
pre[size++] = key;
}

int preIndex = 0;
if (size > 0) {
TreeNode* root = construct_bst(pre, &preIndex, pre[0], INT_MIN, INT_MAX, size);
post_order(root);
}

return 0;
}

0 comments on commit ddd4aec

Please sign in to comment.