-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
khe0613
committed
May 23, 2019
1 parent
f49ea90
commit 5bf625e
Showing
1 changed file
with
46 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# 문제 url -> https://programmers.co.kr/learn/courses/30/lessons/43165 | ||
class Tree: | ||
def __init__(self, target_number): | ||
self.tree_body = [0] | ||
self.target_number = target_number | ||
self.count = 1 # 이진 트리 만들때 사용 | ||
|
||
# 이진 트리 만들기 | ||
def make_tree(self, numbers): | ||
for number in numbers: | ||
for i in range(0, self.count): | ||
self.tree_body.append(number) | ||
self.tree_body.append(-number) | ||
|
||
self.count = self.count * 2 | ||
|
||
# dfs 탐색 | ||
def dfs(self, now_index, now_sum): | ||
now_sum = now_sum + self.tree_body[now_index] | ||
|
||
left_index = 2 * now_index + 1 | ||
right_index = 2 * now_index + 2 | ||
|
||
# now_index가 리프노드 | ||
# 모든 노드의 자식이 0 또는 2이므로, left만 검사해도 됨 | ||
if len(self.tree_body) - 1 < left_index: | ||
if now_sum == self.target_number: | ||
return 1 | ||
else: | ||
return 0 | ||
|
||
|
||
left = self.dfs(left_index, now_sum) | ||
right = self.dfs(right_index, now_sum) | ||
|
||
return left + right | ||
|
||
|
||
|
||
def solution(numbers, target): | ||
tree = Tree(target) | ||
tree.make_tree(numbers) | ||
|
||
answer = tree.dfs(0, 0) | ||
|
||
return answer |