-
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
Showing
4 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
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,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 |
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,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)) |