Adventure Time - Finn 3
본문 바로가기
코테연습/python

이진트리 순회(깊이우선탐색)

by hyun9_9 2026. 5. 11.

전위순회 출력 : 1 2 4 5 3 6 7

import sys
sys.stdin=open("input.txt","rt")
# n,m = map(int,input().split())
# n = int(input())
# arr = list(map(int,input().split()))

def DFS(v):
    if v>7:
        return
    else:
        print(v,end=' ')
        DFS(v*2)
        DFS(v*2+1)



if __name__ =="__main__":
    DFS(1)

 

중위순회 출력 : 4 2 5 1 6 3 7

import sys
sys.stdin=open("input.txt","rt")
# n,m = map(int,input().split())
# n = int(input())
# arr = list(map(int,input().split()))

def DFS(v):
    if v>7:
        return
    else:
        DFS(v*2)
        print(v,end=' ')
        DFS(v*2+1)



if __name__ =="__main__":
    DFS(1)

 

후위순회 출력 : 4 5 2 6 7 3 1

import sys
sys.stdin=open("input.txt","rt")
# n,m = map(int,input().split())
# n = int(input())
# arr = list(map(int,input().split()))

def DFS(v):
    if v>7:
        return
    else:
        DFS(v*2)
        DFS(v*2+1)
        print(v,end=' ')



if __name__ =="__main__":
    DFS(1)

'코테연습 > python' 카테고리의 다른 글

합이 같은 부분집합(DFS : 아마존 인터뷰)  (0) 2026.05.13
부분집합 구하기(DFS)  (0) 2026.05.12
재귀함수를 이용한 이진수 출력  (0) 2026.05.09
최대힙  (0) 2026.05.07
최소힙  (0) 2026.05.06