Description
Given a binary tree, return the vertical order traversal of its nodes’ values. (ie, from top to bottom, column by column).
If two nodes are in the same row and column, the order should be from left to right.
Examples 1:
1 | Input: [3,9,20,null,null,15,7] |
Examples 2:
1 | Input: [3,9,8,4,0,1,7] |
Examples 3:
1 | Input: [3,9,8,4,0,1,7,null,null,null,2,5] (0's right child is 2 and 1's left child is 5) |
解法
在TreeNode外包装一层level,然后通过BFS扫描(不能用前序遍历因为有顺序问题,必须是从上往下的),这里采用TreeMap保证已按Level排好序,遍历输出到list即可。
具体代码如下:
1 | /** |