1. 재귀함수와 stack
import sys
sys.stdin = open("input.txt", "r")

def DFS(x):
		if x>0:
			# print(x)        # 3 2 1
			DFS(x-1)
			print(x, end=' ') # 1 2 3

if __name__== "__main__":
		n = int(input())
		DFS(n

Screenshot 2023-08-20 at 2.56.40 AM.png

Screenshot 2023-08-20 at 2.58.54 AM.png

순서대로 1, D(1), D(2), D(3) 복귀 >> 위에서 부터 하나씩 pop 됨 D(1) 의 복귀주소 > D(2) > D(3) , So output = 1 2 3

  1. 재귀함수를 이용한 이진수 출력
import sys
sys.stdin = open("input.txt", "r")

def DFS(x):                 # x = 11    D(11)    D(5)    D(2)    D(1)    D(0)
		if x == 0:
			 return # 함수를 종료                                                THE END 
		else:                  
			 print(x%2, end=' ') # 1                     1      0       1
			 DFS(x//2)           # 5          D(5)     D(2)    D(1)    D(0)

if __name__ == "__main__":
		n = int(input())
		DFS(n)

________________________________________________________________________________

def DFS(x):                 # x = 11    D(1)    D(5)    D(2)    D(1)    D(0)
		if x == 0:
			 return # 함수를 종료                                                THE END 
		else:                  
			 DFS(x//2)           # 5          D(1)     D(2)    D(1)    D(0)
			 print(x%2, end=' ') # 1            0        1      1

if __name__ == "__main__":
		n = int(input())
		DFS(n)

Screenshot 2023-08-20 at 3.09.41 AM.png

Screenshot 2023-08-20 at 3.10.55 AM.png

  1. 이진트리 순회 Binary Tree (깊이우선탐색 : DFS)

Screenshot 2023-08-20 at 3.12.15 AM.png

Screenshot 2023-08-20 at 3.13.06 AM.png

Screenshot 2023-08-20 at 3.13.59 AM.png

전위: 부 왼 오 | 중위 : 왼 부 오 | 후위: 왼 오 부


Screenshot 2023-08-20 at 3.29.27 AM.png

  1. 부분집합 (dont print 공집합)

Screenshot 2023-08-20 at 3.38.51 AM.png

Screenshot 2023-08-20 at 3.39.34 AM.png

import sys
sys.stdin = open("input.txt", "r")

def DFS(v):
		if v == n+1:
			 for i in range(1, n+1)
					 if ch[i] == 1:
							print(i, end=' ')
					 print()
		else: 
			ch[v] = 1
			DFS(v+1)
			ch[v] = 0
			DFS(v+1)

if __name__ == "__main__":
		n = int(input())
		ch = [0] * (n+1)
		DFS(1)

Screenshot 2023-08-20 at 3.40.22 AM.png

  1. 합이 같은 부분집합 (DFS)

    sum == total - sum

    Screenshot 2023-08-20 at 4.50.24 PM.png