
At Meta, a tree-shaped structure may need to be aggregated by vertical alignment rather than by level. Given the root of a binary tree, return the sum of node values in each vertical column from leftmost column to rightmost column.
A node's column index is defined relative to the root:
0c is in column c - 1c is in column c + 1Implement a function:
root, the root node of a binary tree, represented as nested dictionaries or objects with fields val, left, and right, or null for an empty treeExample 1
Input: root = [1,2,3,4,5,6,7]
Output: [4,2,12,3,7]
Explanation: Columns are -2:[4], -1:[2], 0:[1,5,6], 1:[3], 2:[7].
Example 2
Input: root = [3,9,20,null,null,15,7]
Output: [9,18,20,7]
Explanation: Columns are -1:[9], 0:[3,15], 1:[20], 2:[7].
[0, 10^5]-10^4 <= node.val <= 10^4root = {"val": 1, "left": {"val": 2, "left": {"val": 4, "left": null, "right": null}, "right": {"val": 5, "left": null, "right": null}}, "right": {"val": 3, "left": {"val": 6, "left": null, "right": null}, "right": {"val": 7, "left": null, "right": null}}}Output[4, 2, 12, 3, 7]WhyColumn -2 contains 4, column -1 contains 2, column 0 contains 1+5+6=12, column 1 contains 3, and column 2 contains 7.root = {"val": 3, "left": {"val": 9, "left": null, "right": null}, "right": {"val": 20, "left": {"val": 15, "left": null, "right": null}, "right": {"val": 7, "left": null, "right": null}}}Output[9, 18, 20, 7]WhyNode 9 is in column -1, nodes 3 and 15 share column 0, node 20 is in column 1, and node 7 is in column 2.`0 <= number of nodes <= 10^5``-10^4 <= node.val <= 10^4``root` may be `null`The tree can be skewed or balanceddef vertical_column_sums(root):