Read the Tree Column by Column
Implement columnsLeftToRight
You are given the root of a binary tree. Place the root at position (row 0, column 0); a left child sits one row lower and one column to the left of its parent, and a right child one row lower and one column to the right. Return the node values grouped by column, from the leftmost occupied column to the rightmost. Inside a column list the values from top to bottom; nodes that share both row and column are ordered by value, smaller first.
Handling one column at a time re-walks the tree over and over. Tagging every node with its position, sorting the tags, and grouping by column does the whole job in a single walk plus a sort.
Example 1:
Input: root = [8,4,10,2,6,7,12,1]
Output: [[1],[2],[4],[8,6,7],[10],[12]]
Example 2:
Input: root = [5,3,4]
Output: [[3],[5],[4]]
Example 3:
Input: root = []
Output: []
+ 12 hidden test cases run on Submit.
Constraints:
- ●
0 ≤ number of nodes ≤ 100; −100 ≤ node.val ≤ 100 - ●
The tree is given as its root node (null for an empty tree); each node has a val, a left child and a right child - ●
Give each node a position (row, column): the root is (0, 0); a left child is one row down and one column to the LEFT of its parent (row + 1, column − 1); a right child is one row down and one column to the RIGHT (row + 1, column + 1) - ●
Return one list per occupied column, from the leftmost column to the rightmost. Inside a column list the values from the top row to the bottom row; if two nodes share the same row and column, the smaller value comes first. An empty tree gives an empty list
root =