Loading content...
Given the root node of a binary tree, return the column-wise traversal of all node values. This means you should collect values from top to bottom within each vertical column, iterating through columns from leftmost to rightmost.
A vertical column is defined by its horizontal distance from the root. The root is at column 0, its left child is at column -1, and its right child is at column +1. This pattern continues recursively for all nodes.
Within each column, nodes should appear in the order they would be visited during a level-order (breadth-first) traversal. In other words, if two nodes share the same column index and row index (depth), they should appear from left to right in the original tree structure.
Return the result as a list of lists, where each inner list contains all node values in a single vertical column, ordered from the leftmost column to the rightmost column.
root = [3,9,20,null,null,15,7][[9],[3,15],[20],[7]]The tree has 4 vertical columns. Column -1 contains node 9. Column 0 contains nodes 3 and 15 (from top to bottom). Column 1 contains node 20. Column 2 contains node 7. Reading columns from left to right: [[9],[3,15],[20],[7]].
root = [3,9,8,4,0,1,7][[4],[9],[3,0,1],[8],[7]]Column -2 has node 4. Column -1 has node 9. Column 0 has nodes 3, 0, and 1 (top to bottom in BFS order). Column 1 has node 8. Column 2 has node 7. Note that nodes 0 and 1 are at the same row and column as their parents' children, so they appear in left-to-right order.
root = [1,2,3,4,10,9,11,null,5,null,null,null,null,null,null,null,6][[4],[2,5],[1,10,9,6],[3],[11]]This is a deeper tree with 5 vertical columns. Column -2 has only node 4. Column -1 has nodes 2 and 5 (where 5 is a child of 4). Column 0 has nodes 1, 10, 9, and 6 in level order. Column 1 has node 3. Column 2 has node 11.
Constraints