백준 1991번

2022. 1. 30. 18:56·Study/Algorithm

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

 

1991번: 트리 순회

첫째 줄에는 이진 트리의 노드의 개수 N(1 ≤ N ≤ 26)이 주어진다. 둘째 줄부터 N개의 줄에 걸쳐 각 노드와 그의 왼쪽 자식 노드, 오른쪽 자식 노드가 주어진다. 노드의 이름은 A부터 차례대로 알파

www.acmicpc.net

 

#include <iostream>
#include <map>
using namespace std;

struct treestruct {
	char left;
	char right;
};

map <char, treestruct> tree;

void preorder(char node) {
	if (node == '.') return;
	cout << node;
	preorder(tree.find(node)->second.left);
	preorder(tree.find(node)->second.right);
}

void inorder(char node) {
	if (node == '.') return;
	inorder(tree.find(node)->second.left);
	cout << node;
	inorder(tree.find(node)->second.right);
}

void postorder(char node) {
	if (node == '.') return;
	postorder(tree.find(node)->second.left);
	postorder(tree.find(node)->second.right);
	cout << node;
}

int main() {

	int n;
	cin >> n;
	for (int i = 0; i < n; i++) {
		char node, left, right;
		cin >> node >> left >> right;
		tree.insert({ node, treestruct{left, right} });
	}
	preorder('A');
	cout << endl;
	inorder('A');
	cout << endl;
	postorder('A');
	return 0;
}

설계

'Study > Algorithm' 카테고리의 다른 글

백준 1068번  (0) 2022.02.06
백준 14501번  (0) 2022.01.30
백준 1874번  (0) 2022.01.30
백준 9935번  (0) 2022.01.30
백준 11279번  (0) 2022.01.30
'Study/Algorithm' 카테고리의 다른 글
  • 백준 14501번
  • 백준 1874번
  • 백준 9935번
  • 백준 11279번
_WooHyun_
_WooHyun_
  • _WooHyun_
    Nerd
    _WooHyun_
  • 전체
    오늘
    어제
    • 분류 전체보기 (79)
      • Study (60)
        • Algorithm (24)
        • Unreal Engine (19)
        • C++ (1)
        • Maya (1)
        • GoLang (3)
        • Mysql (3)
        • Linux (7)
        • Server (2)
      • Projects (0)
        • Unreal Engine (0)
        • Server (0)
      • 개발일지 (8)
        • Unreal Engine (7)
        • Art (1)
        • Server (0)
      • 미래 (5)
      • 개발아이디어 (0)
      • 잡지식 (2)
  • 블로그 메뉴

    • 홈
    • 방명록
    • 글쓰기
    • 블로그설정
  • 링크

    • GitHub
  • 공지사항

  • 인기 글

  • 태그

  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
_WooHyun_
백준 1991번
상단으로

티스토리툴바