def solution(numbers, target):
answer = 0
leaves = [0]
for num in numbers:
li = []
for parent in leaves:
li.append(parent + num)
li.append(parent - num)
leaves = li
for leaf in leaves:
if leaf == target:
answer += 1
return answer
leaves 라는 변수로 리스트를 만들고 for문을 돌려서 parent와 num을 더한것을 append할수 있도록 만든다 그리고 leaf와 target이 같을때 answer에 1을 더해나가야 되기 때문에 answer += 1을 하고 마지막으로 출력에서 answer을 출력한다
또한 부모 노드와 자식노드를 for문으로 돌려가면서 각각 leaves를 li로 지정하거나 answer에 1씩 더해간다
'자료구조와 알고리즘' 카테고리의 다른 글
교란 수열 (0) | 2025.05.25 |
---|---|
동적 계획법 (DP) (0) | 2025.03.29 |
[자료구조] 그래프와 트리(Graph, Tree) (0) | 2025.03.26 |
파이썬 힙 함수 (프로그래머스 <명예의 전당(1)> , <더 맵게> , <야근 지수> ) (0) | 2025.03.19 |
DFS/ BFS (0) | 2025.03.16 |