Evaluate N-ary Expression Tree

Evaluate an N-ary tree where internal nodes are operations (sum, product, min, max) and leaves are numbers.

1def evaluate(root: 'NNode') -> int:
2 if root.kind == 'num':
3 return root.num
4 vals = [evaluate(child) for child in root.children]
5 if root.kind == 'sum':
6 return sum(vals)
7 if root.kind == 'product':
8 res = 1
9 for v in vals:
10 res *= v
11 return res
12 if root.kind == 'max':
13 return max(vals)
14 return min(vals)
node
sum
0
1
1
product
2
3
4
2
5
3
6
tree=Array(7)
Step 1 / 3
Step 1:
Evaluate children first, then apply the operation at the node.
Pointers: node=0
Focus: select @ [0]