Rewrite Tree With Subtree Sums
Examples
Example 1:
Input: root1 = [5,2,3,1,4,6,7], root2 = [9,9,9,9,9,9,9]
Output: [28,7,16,1,4,6,7]
Explanation:
Example 2:
Input: root1 = [1,2,3,4,5,6], root2 = [0,0,0,0,0,0]
Output: [21,11,9,4,5,6]
Example 1:
Input: root1 = [5,2,3,1,4,6,7], root2 = [9,9,9,9,9,9,9]
Output: [28,7,16,1,4,6,7]
Explanation:
Example 2:
Input: root1 = [1,2,3,4,5,6], root2 = [0,0,0,0,0,0]
Output: [21,11,9,4,5,6]
You are given the roots of two complete binary trees, root1 and root2.
The two trees have exactly the same structure (same shape and same number of nodes).
For every node position, set the value in root2 to the sum of all values in the corresponding subtree of root1 (including the subtree root itself).
Return the modified root2.
Subtree sums in root1 are: leaves = 1, 4, 6, 7; then 2-subtree = 7, 3-subtree = 16, root = 28.
1 <= Number of nodes <= 10^5-10^4 <= Node.val <= 10^4root1 and root2 are complete binary trees.root1 and root2 have the same structure.