1
+ import java .util .*;
2
+
3
+ class Solution {
4
+ public int [] solution (long [] numbers ) {
5
+ int [] answer = new int [numbers .length ];
6
+
7
+ for (int i =0 ; i <numbers .length ; i ++) {
8
+ long num = numbers [i ];
9
+
10
+ // 0 추가하기
11
+ String ts = makeTree (Long .toBinaryString (num ));
12
+ // System.out.println(ts);
13
+
14
+ // 포화이진트리가 가능한지 체크
15
+ // 부모가 없는데 -> 자식은 있음
16
+ if (checkS (ts )) answer [i ] = 1 ;
17
+ }
18
+
19
+ return answer ;
20
+ }
21
+ public static String makeTree (String s ) {
22
+ int size = s .length ();
23
+ int n = 1 ;
24
+ while (size > (1 << n )-1 ) {
25
+ n ++;
26
+ }
27
+ int cnt = (1 << n ) - size - 1 ;
28
+ return "0" .repeat (cnt ) + s ;
29
+ }
30
+ public static boolean checkS (String now ) {
31
+ // 리프노드
32
+ if (now .length () == 1 ) return true ;
33
+
34
+ int rIdx = now .length () / 2 ;
35
+ // 부모가 없는데 자식이 존재
36
+ String left = now .substring (0 , rIdx );
37
+ String right = now .substring (rIdx +1 );
38
+ if (now .charAt (rIdx ) == '0'
39
+ && (left .contains ("1" ) || right .contains ("1" ))) return false ;
40
+
41
+ // 가능하다면 다시 하위 트리 탐색
42
+ return checkS (left ) && checkS (right );
43
+ }
44
+ }
0 commit comments