[D3] Rooted Binary Tree 재구성 - 7985
성능 요약
메모리: 31,368 KB, 시간: 178 ms, 코드길이: 1,205 Bytes
제출 일자
2023-11-09 09:50
출처: SW Expert Academy, https://swexpertacademy.com/main/code/problem/problemList.do
import java.util.Scanner;
class Solution {
static int [] answer;
static int[] tree;
static int n,k;
public static void main(String args[]) throws Exception {
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
for(int tc = 1; tc <= T; tc++) {
int degree = sc.nextInt();
n = (int)Math.pow(2,degree) - 1;
tree = new int[n];
answer = new int[n];
for(int i = 0; i < n; i++) tree[i] = sc.nextInt();
search(0,n-1,1);
System.out.print("#" + tc + " ");
int i = 0;
int idx = 0;
int count = 0;
while(idx < n){
for(int j = 0; j < Math.pow(2,i); j++){
System.out.print(answer[idx++]+ " ");
}
System.out.println();
i++;
}
}
}
private static void search(int start, int end, int depth){
if(end < 0 || start > n || depth > n) return;
int idx = (start + end) / 2;
answer[depth-1] = tree[idx];
if(start == end) return;
search(start, idx - 1, depth * 2);
search(idx + 1, end, 2 * depth + 1);
}
}
'알고리즘 - SWEA > D3' 카테고리의 다른 글
[SW expert Academy] SWEA 3304번 최장 공통 부분 수열 자바(Java) (0) | 2023.11.10 |
---|---|
[SW expert Academy] SWEA 3282번 0/1 Knapsack 자바(Java) (0) | 2023.11.09 |
[SW expert Academy] SWEA 5607번 조합 자바(Java) (0) | 2023.11.08 |
[SW expert Academy] SWEA 4676번 늘어지는 소리 만들기 자바(Java) (0) | 2023.11.08 |
[SW expert Academy] SWEA 1240번 단순 2진 암호코드 자바(Java) (0) | 2023.11.08 |