base and top. Lay top over base so that the roots line up, then match nodes position by position: left child with left child, right child with right child.
Build a new tree by this rule:
- where both trees have a node at the same position, the result has a node whose value is their sum;
- where only one tree has a node, that node goes into the result together with its whole subtree.
Return the root of the resulting tree (an empty tree if both inputs are empty).base=tree_node([4, 1, 6, None, 2]), top=tree_node([3, -1, None, 5]) → [7, 0, 6, 5, 2]
The roots overlap: 4 + 3 = 7. Left children: 1 + (-1) = 0. That node's left child exists only in top (5) and its right child only in base (2). The root's right child exists only in base (6).
base=tree_node([]), top=tree_node([8, None, 9]) → [8, None, 9]
The first tree is empty, so the result is just the second one.
base=tree_node([2, 2, 2]), top=tree_node([5]) → [7, 2, 2]
Node.val ≤ 10⁴base=tree_node([4, 1, 6, None, 2]), top=tree_node([3, -1, None, 5])
[7, 0, 6, 5, 2]
Run checks the first 3 tests in your browser. Submit checks all 61 tests on the server, hidden ones included.