Vertical Order Traversal of a Binary Tree
update Feb 10 2019, 17:06
Given a binary tree, return the vertical order traversal of its nodes values.
For each node at position (X, Y), its left and right children respectively will be at positions (X-1, Y-1) and (X+1, Y-1).
Running a vertical line from X = -infinity to X = +infinity, whenever the vertical line touches some nodes, we report the values of the nodes in order from top to bottom (decreasing Y coordinates).
If two nodes have the same position, then the value of the node that is reported first is the value that is smaller.
Return an list of non-empty reports in order of X coordinate. Every report will have a list of values of nodes.
Example 1:
Example 2:

Note:
The tree will have between 1 and 1000 nodes.
Each node's value will be between 0 and 1000.
Basic Idea:
与之前类似的一道题目不同,这道题目除了在相同col元素按照从上到下顺序之外另外要求了同样position的元素要按照值的大小从小到大排列。所以基本思路可以使用一个 TreeMap<Integer, TreeSet<int[2]>> 来存每个Node,其中Key是col, int[2]中存的是node.val以及node的depth。TreeMap可以按照col排序,TreeSet可以将每组相同col的node按照从上到下,从小到大的顺序排序。这样我们先用一个dfs populate这个treemap,然后按顺序生成res list即可。时间复杂度为 O(NlogN), 因为需要用到BST。