Save 25%Black Friday: 25% off all plans — use code BLACKFRIDAY2026 at checkout. Ends Monday, Dec 7.Claim the deal

(This question is a variation of the LeetCode question 617. Merge Two Binary Trees. If you haven't completed that question yet, it is recommended to solve it first.)

You are given trees which stand for a list of binary trees. A node's neighbor sum is defined as the sum of the node's value and the values of its left and right children, treating absent children as 0.

Apply the neighbor sum contribution from every tree in trees except the last onto the last tree, in order from left to right:

  • For each position that exists in a source tree, compute its neighbor sum and add it to the value of the corresponding node in the last tree.
  • If the last tree has no node at that position, create one with the neighbor sum as its value.
Stdout
-