Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8f0d62b

Browse files
이지영: [PG] 표현 가능한 이진트리_241206
1 parent 7abaca1 commit 8f0d62b

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

‎Programmers/Level3/JY_150367.java‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /