Given a target sum , we need to find the path count i.e total no of paths. Also it is given that , it is not necessary the path needs to start from the root node.
I came across a solution , for this question. I am also able to understand 85% of the solution.
Solution :
def pathCounter(node,currPath,_sum):
if node is None:
return 0
path = []
currPath.append(node.data)
pathSum , pathCount = 0, 0
#Unable to understand below for loop
for i in range(len(currPath)-1,-1,-1):
pathSum +=currPath[i]
path.append(currPath[i])
if pathSum==_sum:
pathCount+=1
#print(path)
pathCount+= pathCounter(node.left,currPath,_sum)
pathCount+= pathCounter(node.right,currPath,_sum)
del currPath[-1]
return pathCount
The issue I have is, I am unable to understand , why the for loop is set to iterate in reverse order and not in normal order ?
How does this affect the problem ?
path?