← 返回 amazon 的题目列表Binary Tree Vertical Order Traversal
类型:online_judge
Given a binary tree, return its node values in vertical order traversal.
Assign each node a column index:
The root has column 0.
A left child has its parent's column minus 1.
A right child has its parent's column plus 1.
Return the values column by column, from the leftmost column to the rightmost column. Within the same column, nodes must appear in top-to-bottom level-order. If two nodes have the same row and column, preserve their left-to-right order.
The input is given in level-order form, where null represents a missing node.
Example 1
Input: 3 9 20 null null 15 7
Output: [[9],[3,15],[20],[7]]
Example 2
Input: 3 9 8 4 0 1 7
Output: [[4],[9],[3,0,1],[8],[7]]
Constraints
0 <= n <= 10^4
Node values are integers.
Target time complexity: O(n).
Example
Input
3 9 20 null null 15 7
Output
[[9],[3,15],[20],[7]]