#K45122. Greater Sum Tree Transformation
Greater Sum Tree Transformation
Greater Sum Tree Transformation
Given a binary search tree (BST), transform it into a greater sum tree such that every node's new value is equal to the sum of its original value and all node values greater than it. Mathematically, for each node with initial value (v), its new value will be:
[ T(v) = v + \sum_{u > v} u ]
The input BST is represented by a level order traversal, where each token is either an integer or the string null
representing an absent child. The trailing null
values are omitted in the final output. Your task is to output the level order traversal of the resulting greater sum tree.
Example:
If the input is 5 2 13
, the transformed tree's level order traversal is 18 20 13
.
inputFormat
Input is given via standard input. The first and only line contains the level order representation of the BST, where each element is separated by a space. Use the string null
to denote a missing node.
outputFormat
Output the level order representation of the transformed greater sum tree via standard output. The values should be separated by a space, and trailing null
values (that don't affect the structure) must be removed.## sample
5 2 13
18 20 13