[백준] 5639번: 이진 검색 트리

박정훈·2022년 2월 14일
0

코테 문제 모음

목록 보기
13/34

백준 5639번

import sys
sys.setrecursionlimit(10**6)

def postorder(start, end):
    if start > end: return
    root = pre_order[start]
    idx = start + 1
    
    for i in range(idx, end + 1):
        if pre_order[i] > root:
            idx = i
            break
    postorder(start + 1, idx -1)
    postorder(idx, end)
    print(root)
    
pre_order = []
while True:
    try:
        pre_order.append(int(sys.stdin.readline()))
    except:
        break

postorder(0, len(pre_order) - 1)

입력에 정해진 것이 없는 경우의 방법과 RecursionError를 새로 알게 되었다. 제한을 늘려줘야 했다.

profile
그냥 개인적으로 공부한 글들에 불과

0개의 댓글