Back to problems

Rewrite Tree With Subtree Sums

Algorithm · Snowflake · Medium

Coding — Software Engineer You receive the roots of two complete binary trees, root1 and root2. Both trees share an identical shape and contain the same number of nodes. Update each position in root2 with the sum of every value in the matching subtree of root1, counting the subtree's root as well. Return root2 after these updates. 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: The leaf subtree totals are 1,…

Checking your access…