[BOJ] 1991번: 트리 순회

2023. 8. 26. 11:22·Problem Solving/Baekjoon

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

 

1991번: 트리 순회

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

www.acmicpc.net

 

 

node 클래스를 만들어서 left 자식과 right 자식을 관리해주면 된다. 쉬운 문제였다 !

package com.ll.boj.silver.p1991;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

public class Main {
    static class node {
        int left;
        int right;

        public node(int left, int right) {
            this.left = left;
            this.right = right;
        }
    }
    static List<node>[] graph;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        sc.nextLine();

        graph = new List[n];
        for(int i=0; i<n; i++) graph[i] = new ArrayList<>();


        for(int i=0; i<n; i++) {
            String[] tmp = sc.nextLine().split(" ");
            int root = tmp[0].charAt(0) - 'A';
            int left = tmp[1].charAt(0) - 'A';
            int right = tmp[2].charAt(0) - 'A';
            graph[root].add(new node(left, right));

        }
        preorder(0);
        System.out.println();

        inorder(0);
        System.out.println();

        postorder(0);
    }

    private static void postorder(int start) {
        for(node x : graph[start]) {
            if (x.left != -19) postorder(x.left);
            if (x.right != -19) postorder(x.right);
            System.out.print((char)(start + 'A'));
        }
    }

    private static void inorder(int start) {
        for(node x : graph[start]) {
            if (x.left != -19) inorder(x.left);
            System.out.print((char)(start + 'A'));
            if (x.right != -19) inorder(x.right);
        }
    }

    private static void preorder(int start) {
        for(node x : graph[start]) {
            System.out.print((char)(start + 'A'));
            if (x.left != -19) preorder(x.left);
            if (x.right != -19) preorder(x.right);
        }
    }
}

 

'Problem Solving/Baekjoon' 카테고리의 다른 글
  • [BOJ] 4485번 : 녹색 옷 입은 애가 젤다지?
  • [BOJ] 2252번 : 줄 세우기
  • [BOJ] 3085번: 사탕 게임
  • [BOJ] 1476번: 날짜 계산
kimdozzi
kimdozzi
끝까지 포기하지 않으면, 내가 다 이겨!
  • kimdozzi
    도브로
    kimdozzi
  • 전체
    오늘
    어제
    • 분류 전체보기 (132)
      • Problem Solving (49)
        • Baekjoon (29)
        • Programmers (0)
        • LeetCode (17)
        • 삼성 유형 (2)
      • Computer Science (27)
        • Operating System (2)
        • Algorithms (13)
        • Network (6)
        • DataBase (6)
      • Backend (33)
        • JavaScript (0)
        • TypeScript (6)
        • Java (7)
        • Spring Boot (7)
        • Spring Security (6)
        • JPA (2)
        • Mybatis (1)
        • Junit5 (1)
        • Redis (3)
      • DevOps (14)
        • Git, Github (5)
        • docker (4)
        • AWS (3)
        • nginx (2)
      • etc (6)
        • IntelliJ (3)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
    • 티스토리
    • 설정
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    타입스크립트
    세그먼트 트리
    PrefixSum
    구간 업데이트
    온라인 쿼리
    파이썬
    Bucket
    점 업데이트
    컨테이너
    오블완
    imos법
    segment tree
    TypeScript
    누적합
    구간합
    docker
    도커
    python
    삼성기출
    오프라인 쿼리
    S3
    docker image
    인덱서블 타입
    interface
    백준
    인터페이스
    티스토리챌린지
    AWS
    알고리즘
    CORS
    인덱스 시그니처
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.0
kimdozzi
[BOJ] 1991번: 트리 순회
상단으로

티스토리툴바