-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path표현가능한이진트리.java
56 lines (42 loc) · 1.48 KB
/
표현가능한이진트리.java
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
class Solution {
public int[] solution(long[] numbers) {
int[] answer = new int[numbers.length];
for (int i = 0; i < numbers.length; i++) {
answer[i] = expressible(numbers[i]) ? 1 : 0;
}
return answer;
}
private boolean expressible(long number) {
int height = findHeightOf(number);
int length = (int) (Math.pow(2, height) - 1);
int[] bits = toBinary(number, length);
return expressible(bits, 0, length);
}
private int findHeightOf(long number) {
int height = 1;
while (Math.pow(2, Math.pow(2, height) - 1) - 1 < number) {
height++;
}
return height;
}
private int[] toBinary(long number, int length) {
int[] bits = new int[length];
for (int i = length - 1; i >= 0; i--) {
bits[i] = (int) (number & 1L);
number >>= 1;
}
return bits;
}
private boolean expressible(int[] bits, int left, int right) {
if (left + 1 == right) {
return true;
}
int root = (left + right) / 2;
int leftRoot = (left + root) / 2;
int rightRoot = (root + right) / 2;
if (bits[root] == 0 && (bits[leftRoot] == 1 || bits[rightRoot] == 1)) {
return false;
}
return expressible(bits, left, root) && expressible(bits, root + 1, right);
}
}