백준 5639번 (이진 검색 트리, C++) [BAEKJOON]

이진 검색 트리

https://www.acmicpc.net/problem/5639

시간 제한메모리 제한제출정답맞힌 사람정답 비율
1 초256 MB3544213888982138.260%

문제

이진 검색 트리는 다음과 같은 세 가지 조건을 만족하는 이진 트리이다.

  • 노드의 왼쪽 서브트리에 있는 모든 노드의 키는 노드의 키보다 작다.
  • 노드의 오른쪽 서브트리에 있는 모든 노드의 키는 노드의 키보다 크다.
  • 왼쪽, 오른쪽 서브트리도 이진 검색 트리이다.

전위 순회 (루트-왼쪽-오른쪽)은 루트를 방문하고, 왼쪽 서브트리, 오른쪽 서브 트리를 순서대로 방문하면서 노드의 키를 출력한다.

후위 순회 (왼쪽-오른쪽-루트)는 왼쪽 서브트리, 오른쪽 서브트리, 루트 노드 순서대로 키를 출력한다.

예를 들어, 위의 이진 검색 트리의 전위 순회 결과는 50 30 24 5 28 45 98 52 60 이고, 후위 순회 결과는 5 28 24 45 30 60 52 98 50 이다.

이진 검색 트리를 전위 순회한 결과가 주어졌을 때, 이 트리를 후위 순회한 결과를 구하는 프로그램을 작성하시오.

입력

트리를 전위 순회한 결과가 주어진다.

노드에 들어있는 키의 값은 106보다 작은 양의 정수이다.

모든 값은 한 줄에 하나씩 주어지며, 노드의 수는 10,000개 이하이다.

같은 키를 가지는 노드는 없다.

출력

입력으로 주어진 이진 검색 트리를 후위 순회한 결과를 한 줄에 하나씩 출력한다.

예제 입력 1

50
30
24
5
28
45
98
52
60

예제 출력 1

5
28
24
45
30
60
52
98
50

출처

ICPC > Regionals > Asia Pacific > Thailand > 2011 ACM-ICPC Asia Phuket Regional Programming Contest B번

알고리즘 분류


통과된 코드

#include <iostream>

using namespace std;

struct BinaryNode
{
    int _Num;
    BinaryNode* _Left;
    BinaryNode* _Right;

    BinaryNode(int num)
        : _Num(num), _Left(nullptr), _Right(nullptr)
    {}

    void ArrayBinaryNode(int _n)
    {
        if (_Num > _n) {
            if (_Left == nullptr) _Left = new BinaryNode(_n);
            else _Left->ArrayBinaryNode(_n);
        }
        else {
            if (_Right == nullptr) _Right = new BinaryNode(_n);
            else _Right->ArrayBinaryNode(_n);
        }
    }
};

void PostorderTraverse(BinaryNode& CurrentNode)
{
    if (CurrentNode._Left != nullptr) PostorderTraverse(*CurrentNode._Left);
    if (CurrentNode._Right != nullptr) PostorderTraverse(*CurrentNode._Right);
    cout << CurrentNode._Num << "\n";
}


int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    int _N;
    cin >> _N;
    BinaryNode _RootNode(_N);
    while (true) {
        cin >> _N;
        if (cin.eof() == true)
            break;
        _RootNode.ArrayBinaryNode(_N);
    }

    PostorderTraverse(_RootNode);

	return 0;
}

“백준 5639번 (이진 검색 트리, C++) [BAEKJOON]”에 대한 1,478개의 생각

  1. http://felixrbhu869.image-perth.org/one-pot-curry-recipes-quick-and-easy-meals-for-busy-days

    Hey everyone! Just wanted to jump in and share my experience with curry powder lately—it’s quickly becoming one of my go-to spices, not just for flavor but for the health benefits as well. I’ve always loved the taste of curry in different dishes, but I recently started using curry powder more intentionally, and I’m seeing some great benefits from it.

    For starters, I’ve been adding it to my meals a few times a week—everything from soups and stews to roasted veggies and even scrambled eggs. What I’ve noticed, aside from the amazing flavor boost, is that it’s been helping with my digestion. After reading up on it, I found that the spices in curry powder—especially turmeric—are great for gut health and reducing inflammation. I’ve always had mild issues with bloating after meals, but it seems like since using curry powder more consistently, that’s improved a bit.

    Another thing that’s been interesting is its potential anti-inflammatory benefits. I’ve had some minor joint pain from workouts, and since using curry powder regularly, it feels like there’s been less stiffness, especially in the mornings. Plus, it’s just such an easy way to add extra nutrients like antioxidants to my meals without much effort.

    If you’re looking to spice up your meals and get some health benefits in the process, I definitely recommend giving curry powder a try. It’s such a simple addition, but it’s made a noticeable difference for me. Has anyone else been using it for its health perks? I’d love to hear how others are incorporating it into their diets or any favorite recipes you’ve found!

  2. http://emilianohrei730.tearosediner.net/top-10-health-benefits-of-adding-curry-powder-to-your-diet

    Hey everyone! Just wanted to jump in and share my experience with curry powder lately—it’s quickly becoming one of my go-to spices, not just for flavor but for the health benefits as well. I’ve always loved the taste of curry in different dishes, but I recently started using curry powder more intentionally, and I’m seeing some great benefits from it.

    For starters, I’ve been adding it to my meals a few times a week—everything from soups and stews to roasted veggies and even scrambled eggs. What I’ve noticed, aside from the amazing flavor boost, is that it’s been helping with my digestion. After reading up on it, I found that the spices in curry powder—especially turmeric—are great for gut health and reducing inflammation. I’ve always had mild issues with bloating after meals, but it seems like since using curry powder more consistently, that’s improved a bit.

    Another thing that’s been interesting is its potential anti-inflammatory benefits. I’ve had some minor joint pain from workouts, and since using curry powder regularly, it feels like there’s been less stiffness, especially in the mornings. Plus, it’s just such an easy way to add extra nutrients like antioxidants to my meals without much effort.

    If you’re looking to spice up your meals and get some health benefits in the process, I definitely recommend giving curry powder a try. It’s such a simple addition, but it’s made a noticeable difference for me. Has anyone else been using it for its health perks? I’d love to hear how others are incorporating it into their diets or any favorite recipes you’ve found!

  3. https://list.ly/i/10179516

    Hey there, cinnamon lovers! If you’re as obsessed with Ceylon cinnamon toothpicks as I am, you’ve got to check out this incredible resource I found. It’s like a candy shop for anyone who appreciates the magic of these little sticks.

    So, what makes these toothpicks so special? For starters, they’re made from genuine Ceylon cinnamon—none of that common Cassia stuff. The difference is like night and day; Ceylon cinnamon has this sweet, delicate flavor that just elevates your toothpick game. Whether you’re into a subtle cinnamon kiss or a more intense burst of flavor, this site has you covered.

    But here’s the kicker: it’s not just about the products. This place goes all out with tons of info on why Ceylon cinnamon is so awesome. You’ll find all the juicy details on its benefits, like how it helps with oral health and freshens up your breath. Plus, their blog is packed with creative tips on how to use these toothpicks in everyday life—think fancy cocktail garnishes or cool homemade gifts.

    So if you’re curious about Ceylon cinnamon toothpicks or just want to dive deeper into the world of these spicy little wonders, this site is your new best friend. It’s got everything you need, from top-quality products to all the background info you could want. Trust me, it’s worth a visit if you’re ready to up your toothpick game!

  4. http://emilianolibj473.lucialpiazzale.com/exploring-different-types-of-curry-across-asia

    Hey everyone! Just wanted to jump in and share my experience with curry powder lately—it’s quickly becoming one of my go-to spices, not just for flavor but for the health benefits as well. I’ve always loved the taste of curry in different dishes, but I recently started using curry powder more intentionally, and I’m seeing some great benefits from it.

    For starters, I’ve been adding it to my meals a few times a week—everything from soups and stews to roasted veggies and even scrambled eggs. What I’ve noticed, aside from the amazing flavor boost, is that it’s been helping with my digestion. After reading up on it, I found that the spices in curry powder—especially turmeric—are great for gut health and reducing inflammation. I’ve always had mild issues with bloating after meals, but it seems like since using curry powder more consistently, that’s improved a bit.

    Another thing that’s been interesting is its potential anti-inflammatory benefits. I’ve had some minor joint pain from workouts, and since using curry powder regularly, it feels like there’s been less stiffness, especially in the mornings. Plus, it’s just such an easy way to add extra nutrients like antioxidants to my meals without much effort.

    If you’re looking to spice up your meals and get some health benefits in the process, I definitely recommend giving curry powder a try. It’s such a simple addition, but it’s made a noticeable difference for me. Has anyone else been using it for its health perks? I’d love to hear how others are incorporating it into their diets or any favorite recipes you’ve found!

댓글 달기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다

위로 스크롤