Haribo ML, AI, MATH, Algorithm

표현 가능한 이진트리

2023-11-23
Haribo

표현 가능한 이진트리

코드

def expand_bin(bin_number) :
    i = 0
    while len(bin_number) > 2**(i+1) - 1 :
        i += 1
    return '0'*(2**(i+1) - 1 - len(bin_number)) + bin_number

def possiblity_check(bin_number) :
    center = len(bin_number) // 2
    if len(bin_number) == 1 :
        return True
    elif bin_number[center] == '0' :
        return not ('1' in bin_number[:center] or '1' in bin_number[center + 1:])
    else:
        return possiblity_check(bin_number[:center]) and possiblity_check(bin_number[center + 1:])

def solution(numbers):
    answer = []
    for number in numbers :
        bin_number = str(bin(number))[2:]
        bin_number = expand_bin(bin_number)
        answer.append(possiblity_check(bin_number)*1)
    return answer

Similar Posts

다음 포스트 표 병합

Comments