Question
Please help solve this in Python!! You are given an input file with multiple pairs of input lines. The first line of each pair is
Please help solve this in Python!!
You are given an input file with multiple pairs of input lines. The first line of each pair is a tree given as a predecessor array. The second line is the value at the corresponding node. Values at leaf nodes (nodes with no children) are integers. At non-leaf nodes, the two possible values are + or .
The tree represents an arithmetic expression where the value at a non-leaf node u is the sum of values at the children of u in the case of +, or the product of values at the children of u in the case of .
You need to calculate the value at each node and output the calculated value at the root. The tree is not constrained to be binary.
Input format:
Input consists of m pairs of lines of comma separated values, so 2m lines in total. The first line is each pair is a comma separated list of integers representing a tree in predecessor array format where 1 represents null.
The second line in each pair is a comma separated list of integers and the symbols + and . The ith item on the list is the value or operator at the ith node in the tree.
For example:
-1,0,0,0,1,1
+,*,2,3,0,7
2,0,-1,0
+,3,*,3
Output format:
For each pair of input lines, output a line containing the value calculated at the root of the tree.
For the example input above, output would be:
5
6
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started