Given a binary tree with transition weights.
It is necessary to get the maximum value for N transitions starting from the zero node. In this case, the path must be continuous, with free returns to the parent node.
My current algorithm is:
- Find the last available node (in which the number of moves is 0, or there are no children).
- Write to the array the current value and the number of available moves (including descendants).
- Go to the parent node, copy both arrays (if 2 children), or 1 array (1 child).
- Repeat step 2-3 to return to the root node.
- Based on the array in the root node (which contains all possible combinations of values + moves) find the maximum value.
Main questions:
- Is there a better algorithm?
- Is it possible to optimize this algorithm? works too long and requires too much memory?