Skip to content
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

18-YIM2UL2ET #55

Merged
merged 6 commits into from
Jun 27, 2024
Merged

18-YIM2UL2ET #55

merged 6 commits into from
Jun 27, 2024

Conversation

YIM2UL2ET
Copy link
Collaborator

@YIM2UL2ET YIM2UL2ET commented Apr 19, 2024

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

์ด์ง„ ๊ฒ€์ƒ‰ ํŠธ๋ฆฌ - BOJ 5639

โœ”๏ธ ์†Œ์š”๋œ ์‹œ๊ฐ„

2h

โœจ ์ˆ˜๋„ ์ฝ”๋“œ

1. ๋ฌธ์ œ ์„ค๋ช…

  • ๋…ธ๋“œ์˜ ์™ผ์ชฝ ์„œ๋ธŒํŠธ๋ฆฌ์— ์žˆ๋Š” ๋ชจ๋“  ๋…ธ๋“œ์˜ ํ‚ค๋Š” ๋…ธ๋“œ์˜ ํ‚ค๋ณด๋‹ค ์ž‘๋‹ค.
  • ๋…ธ๋“œ์˜ ์˜ค๋ฅธ์ชฝ ์„œ๋ธŒํŠธ๋ฆฌ์— ์žˆ๋Š” ๋ชจ๋“  ๋…ธ๋“œ์˜ ํ‚ค๋Š” ๋…ธ๋“œ์˜ ํ‚ค๋ณด๋‹ค ํฌ๋‹ค.
  • ์™ผ์ชฝ, ์˜ค๋ฅธ์ชฝ ์„œ๋ธŒํŠธ๋ฆฌ๋„ ์œ„ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ํŠธ๋ฆฌ์ด๋‹ค

์ด๋Ÿฌํ•œ ํŠธ๋ฆฌ๋ฅผ ์ „์œ„์ˆœํšŒํ•œ ๊ฒฐ๊ณผ๋ฅผ ์ž…๋ ฅ๋ฐ›๊ณ , ํ›„์œ„์ˆœ์œ„๋ฅผ ํ•˜์—ฌ ์ถœ๋ ฅํ•˜๋ฉด ๋œ๋‹ค.

2. ๋ฌธ์ œ ๋ถ„์„

์ž…๋ ฅ ๋ฐ›๊ธฐ

์ „์œ„ ์ˆœํšŒ ๋ฐฉ์‹์€ ๋ฃจํŠธ๋…ธ๋“œ -> ์ขŒ์ธก๋…ธ๋“œ -> ์šฐ์ธก๋…ธ๋“œ ์ด๋‹ค.
๊ทธ๋ ‡๋‹ค๋ฉด ์ž…๋ ฅ๋„ ๋ฃจํŠธ๋…ธ๋“œ๋ถ€ํ„ฐ ์ž…๋ ฅ์„ ๋ฐ›์œผ๋ฉด ๋  ๊ฒƒ์ด๋‹ค.

๋…ธ๋“œ์˜ ๊ฐ’์„ ์ž…๋ ฅ ๋ฐ›์„ ๋•Œ ๋…ธ๋“œ๋“ค์€ ์ „๋ถ€ ๋‹ค์Œ ์กฐ๊ฑด์„ ๋งŒ์กฑ์‹œ์ผœ์•ผ ํ•œ๋‹ค.

  • ์˜ค๋ฅธ์ชฝ ์„œ๋ธŒํŠธ๋ฆฌ๋Š” ์ž์‹ ์˜ ๊ฐ’๋ณด๋‹ค ๋ชจ๋‘ ํฌ๋‹ค.
  • ์™ผ์ชฝ ์„œ๋ธŒํŠธ๋ฆฌ๋Š” ์ž์‹ ์˜ ๊ฐ’๋ณด๋‹ค ๋ชจ๋‘ ์ž‘๋‹ค.

๋”ฐ๋ผ์„œ ๋ฃจํŠธ๋…ธ๋“œ์— ๊ฐ’์ด ์กด์žฌํ•œ๋‹ค๋ฉด ์™ผ์ชฝ ์ž์‹ ๋…ธ๋“œ์™€, ์˜ค๋ฅธ์ชฝ ์ž์‹ ๋…ธ๋“œ๋ฅผ ์ž…๋ ฅ๋ฐ›์•„์•ผ ํ•˜๋Š”๋ฐ, ์ด ์กฐ๊ฑด์„ ์ด์šฉํ•˜์—ฌ ์ž…๋ ฅ๋ฐ›์œผ๋ฉด ๋  ๊ฒƒ์ด๋‹ค.

Input ํ•จ์ˆ˜ (๋…ธ๋“œ์˜ ์ฃผ์†Œ, ๊ฐ’):
    if ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ž…๋ ฅ๋ฐ›์€ ๋…ธ๋“œ์— ๊ฐ’์ด ์—†์œผ๋ฉด: 
        ๋…ธ๋“œ์— ๊ฐ’์„ ์ž…๋ ฅ
    else if ๋งค๊ฐœ๋ณ€์ˆ˜ ๋…ธ๋“œ์˜ ๊ฐ’ > ๋งค๊ฐœ๋ณ€์ˆ˜ ๊ฐ’ :
        Input(์ขŒ์ธก ๋…ธ๋“œ์˜ ์ฃผ์†Œ, ๊ฐ’)
    else
        Input(์šฐ์ธก ๋…ธ๋“œ์˜ ์ฃผ์†Œ, ๊ฐ’)

์ถœ๋ ฅํ•˜๊ธฐ

์ž…๋ ฅ์„ ๋‹ค ๋ฐ›์•˜๋‹ค๋ฉด ํŠธ๋ฆฌ๊ฐ€ ์™„์„ฑ๋˜์—ˆ์„ ๊ฒƒ์ด๋ฏ€๋กœ ๋ฃจํŠธ ๋…ธ๋“œ์˜ ์ฃผ์†Œ๋ฅผ ์ด์šฉํ•˜์—ฌ ํ›„์œ„์ˆœํšŒ๋ฅผ ํ•˜๋ฉด ๋œ๋‹ค.
ํ›„์œ„ ์ˆœํšŒ๋Š” ์ขŒ์ธก๋…ธ๋“œ -> ์šฐ์ธก ๋…ธ๋“œ -> ๋ฃจํŠธ ๋…ธ๋“œ ์ด๋‹ค.
์ด๋Š” ๊ฐ„๋‹จํ•˜๊ฒŒ ๊ตฌํ˜„์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

Output ํ•จ์ˆ˜ (๋…ธ๋“œ์˜ ์ฃผ์†Œ):
    if ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ž…๋ ฅ๋ฐ›์€ ๋…ธ๋“œ์— ์™ผ์ชฝ ์ž์‹ ๋…ธ๋“œ๊ฐ€ ์žˆ๋‹ค๋ฉด:
        Output(์ขŒ์ธก ๋…ธ๋“œ์˜ ์ฃผ์†Œ)
    if ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ž…๋ ฅ๋ฐ›์€ ๋…ธ๋“œ์— ์˜ค๋ฅธ์ชฝ ์ž์‹ ๋…ธ๋“œ๊ฐ€ ์žˆ๋‹ค๋ฉด:
        Output(์šฐ์ธก ๋…ธ๋“œ์˜ ์ฃผ์†Œ)
    ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ž…๋ ฅ๋ฐ›์€ ๋…ธ๋“œ์˜ ๊ฐ’ ์ถœ๋ ฅ

3. ์ตœ์ข… ์ฝ”๋“œ

#include <iostream>
using namespace std;

struct node {
    int value = -1;
    node* right = NULL;
    node* left = NULL;
};

void input(node &nd, int n) {
    if (nd.value == -1) nd.value = n;
    else if (nd.value > n) {
        if (nd.left == NULL) nd.left = new node;
        input(*nd.left, n); 
    }
    else {
        if (nd.right == NULL) nd.right = new node;
        input(*nd.right, n); 
    }
}

void output(node &nd) {
    if (nd.left != NULL) output(*nd.left);
    if (nd.right != NULL) output(*nd.right);
    cout << nd.value << '\n';
}

int main(void) {
    int n;
    node root;
    while (cin >> n) input(root, n);
    output(root);
    return 0;
}

๐Ÿ“š ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ๋œ ๋‚ด์šฉ

ํŠธ๋ฆฌ์—์„œ ์ž…๋ ฅ ๋ฐ›๋Š” ํ…Œํฌ๋‹‰: ์ˆœํšŒ ๋ฐฉ์‹์„ ์ž˜ ๋ณด๊ณ  ์ž…๋ ฅ ๋ฐ›๋Š” ๋ฐฉ์‹์„ ์ฐพ์ž.
์ „์œ„์ˆœํšŒ๋Š” ๋ฃจํŠธ๋…ธ๋“œ์—์„œ ์ž…๋ ฅ๋ฐ›์œผ๋ฉด ๊ตฟ

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants