Transform tree using counterpart subtree sums
Company: Snowflake
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: Medium
Interview Round: Technical Screen
Quick Answer: This question evaluates proficiency with binary tree algorithms and in-place subtree aggregation, emphasizing traversal strategies, shape verification, and handling subtree sums under linear-time and O(h) space constraints.
Constraints
- Trees have identical array shape
Examples
Input: ([1, 2, 3], [0, 0, 0])
Expected Output: [6, 2, 3]
Explanation: Root sum and leaf sums.
Input: ([5], [9])
Expected Output: [5]
Explanation: Single node.
Input: ([1, 2, 3, 4, 5, 6, 7], [0, 0, 0, 0, 0, 0, 0])
Expected Output: [28, 11, 16, 4, 5, 6, 7]
Explanation: Full tree.
Hints
- Postorder traversal computes each subtree sum once.