I have some problems when I write the recursive function.
The problem is finding the maximum total value in binary tree.
And each node can choose maximum child node.
For example, Node 8 can choose 1 and 0, then add 1.
My code is here,
triangle = [[7], [3, 8], [8, 1, 0], [2, 7, 4, 4], [4, 5, 2, 6, 5]]
#result = 30
depth = 1
answer = triangle[0][0] # depth : 0
total = 0
N1,N2 = triangle[1][0], triangle[1][1]
And, I write Recursive Functions like this,
def min_max(depth, N, total):
if depth == len(triangle):
return total
else:
a,b = triangle[depth+1][triangle[depth].index(N)], triangle[depth+1][triangle[depth].index(N)+1]
total += max(a,b)
N = max(a,b)
return min_max(depth+1, N, total)
min_max(depth, N1, total)
But, I got list index out of range.
How can I fix recursive function?


if depth+1 == len(triangle)and you will not crash, but not sure if other logic is correct.