Jun's Development Journey
[SWEA] 1231. [S/W 문제해결 기본] Tree - 중위순회 본문
문제
SW Expert Academy
SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!
www.swexpertacademy.com
풀이
이 문제는 주어지는 자료를 토대로 트리를 생성해서 중위순회하여 출력하는 문제이다.
문제 조건에서 정점의 갯수가 주어지기 때문에 정적 배열을 이용해서 트리를 구현했다. 왼쪽 자식은 인덱스*2, 오른쪽 자식은 인덱스*2 +1 를 이용해서 구현했다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
public class Solution {
static Scanner scan = new Scanner(System.in);
static char[] nodes;
static int num;
static void solution() {
for (int i = 1; i <= 10; i++) {
num = scan.nextInt();
nodes = new char[num+1];
scan.nextLine();
for(int j=1;j<=num;j++) {
String tmp = scan.nextLine();
nodes[j] = (tmp.split(" ")[1]).charAt(0);
}
System.out.print("#"+i+" ");
in_order(1);
System.out.println();
}
}
static void in_order(int idx) {
if(is_valid(idx)) {
in_order(idx*2);
System.out.print(nodes[idx]);
in_order(idx*2+1);
}
}
static boolean is_valid(int idx) {
if(idx>num)
return false;
return true;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
solution();
}
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
|
'SWEA > Intermediate' 카테고리의 다른 글
[SWEA] 1232. [S/W 문제해결 기본] Tree - 사칙연산 (0) | 2019.08.07 |
---|---|
[SWEA] 1233. [S/W 문제해결 기본] Tree - 사칙연산 유효성 검사 (0) | 2019.08.07 |
[SWEA] 1230. [S/W 문제해결 기본] List - 암호문3 (0) | 2019.08.06 |
[SWEA] 1229. [S/W 문제해결 기본] List - 암호문2 (0) | 2019.08.06 |
[SWEA] 1228. [S/W 문제해결 기본] List - 암호문1 (0) | 2019.08.06 |