Skip to content

Commit

Permalink
solve TargetNumber
Browse files Browse the repository at this point in the history
  • Loading branch information
Junwon6 committed May 20, 2019
1 parent 3718502 commit 86c4add
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 0 deletions.
File renamed without changes.
File renamed without changes.
36 changes: 36 additions & 0 deletions 박준원/etc/TargetNumber solution.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
Ÿ�� �ѹ�

[input]
numbers
target

[output]
����� ����

--------------------------------------------------------

���ϰ� ���� �ΰ��� ���

{a, b, c, d}

struct node {
node left
node right
int value
}


0
2 a -a
4 b -b b -b
8 c -c c -c c -c c -c
16 d -d d -d d -d d -d d -d


1 + 2 + 4 + 8 + 16
==> maketree

==> find target
root���� leaf���� ���� ��� ����� ���� Ž����
��� ����� ���� sum�� ����
target�� ������ count
35 changes: 35 additions & 0 deletions 박준원/etc/TargetNumber.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
class Node:
def __init__(self, num):
self.value = num
self.left = None
self.right = None

def buildTree(node, num):
if node.left == None and node.right == None:
node.left = Node(num)
node.right = Node(-num)
return

buildTree(node.left, num)
buildTree(node.right, num)

def count(node, sum, target):
if node.left == None and node.right == None:
temp = sum + node.value
if temp == target:
return 1
else:
return 0

return count(node.left, node.value + sum, target) + count(node.right, node.value + sum, target)


numbers = [1, 2, 3, 4, 5]
target = 15

root = Node(0)

for num in numbers:
buildTree(root, num)

print(count(root, 0, target))

0 comments on commit 86c4add

Please sign in to comment.