
Sum Tree Conversion
Convert a given binary tree into its sum tree. In a sum tree, every node's value is replaced with the sum of its immediate children's values. Leaf nodes are set to 0. Finally, return the preorder traversal of the sum tree.
Input:
Elements of the input are given in level order traversal. Each node value is given on a new line. Use -1 to represent null nodes.
Output:
Output the preorder traversal of the converted sum tree.
Example:
Input:
1
2
3
4
-1
5
6
-1
7
-1
-1
-1
-1
-1
-1
Explanation:
The binary tree given in the input can be organized based on the level order as depicted in the image provided. The conversion involves changing each node's value except leaves to the sum of its children and setting the leaves to 0. Once the tree is converted, return its preorder traversal.
Constraints:
0 <= Number of nodes <= 10^7
0 <= Value of node <= 10^8


Convert a binary tree into a sum tree by replacing each node's value with the sum of its children's values. Return the preorder traversal of the sum tree.
Traverse the tree in a bottom-up manner to cal...read more

Popular interview questions of Full Stack Developer
Reviews
Interviews
Salaries
Users/Month