aboutsummaryrefslogtreecommitdiff
path: root/challenge-056/user-person/python/ch-2.py
blob: 3d7c61b44570e328c43c647a30b5abb12d071a20 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/env python3

###########################################################################
# script name: ch-2.py                                                    #
#                                                                         #
# https://github.com/user-person                                          #
#                                                                         #
# https://perlweeklychallenge.org/blog/perl-weekly-challenge-056/         #
#                                                                         #
# Path Sum                                                                #
# You are given a binary tree and a sum, write a script to find if        #
# the tree has a path such that adding up all the values along the path   #
# equals the given sum. Only complete paths (from root to leaf node) may  #
# be considered for a sum.                                                #
#                                                                         #
# Example                                                                 #
# Given the below binary tree and sum = 22,                               #
#                                                                         #
#             5                                                           #
#            / \                                                          #
#           4   8                                                         #
#          /   / \                                                        #
#         11  13  9                                                       #
#        /  \      \                                                      #
#       7    2      1                                                     #
#                                                                         #
# For the given binary tree, the partial path sum 5 -> 8 -> 9 = 22 is     #
# not valid.                                                              #
# The script should return the path 5 -> 4 -> 11 -> 7 whose sum is 22     #
#                                                                         #
###########################################################################

class Node:
    def __init__(self,key):
        self.left  = None
        self.right = None
        self.val   = key

branchMemory = []
k = 22
match = 0

def preOrder(root, toLeafSum):
    if root:
        toLeafSum += root.val
        if root.left != None and root.right != None:
            branchMemory.append(toLeafSum)
        if root.left == None and root.right == None:
            if toLeafSum == k:
                global match
                match += 1
            if len(branchMemory) > 0:
                toLeafSum = branchMemory.pop()
        preOrder(root.left,  toLeafSum)
        preOrder(root.right, toLeafSum)

bTree = Node(5)
bTree.left = Node(4)
bTree.left.left = Node(11)
bTree.left.left.left  = Node(7)
bTree.left.left.right = Node(2)
bTree.right = Node(8)
bTree.right.left  = Node(13)
bTree.right.right = Node(9)
bTree.right.right.right = Node(1)

tLS = 0
preOrder(bTree,tLS)

if  match == 0:
    print('No branch equals', k)
elif match == 1:
    print('1 branch equals', k)
else:
    print(match, 'branches equal', k)

# output:
#
# 1 branch equals 22