Binary Tree Vertical Order Traversal

Given a binary tree, return thevertical ordertraversal 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.

Example

Examples 1:

Input:
[3,9,20,null,null,15,7]

   3
  /\
 /  \
 9  20
    /\
   /  \
  15   7 

Output:
[
  [9],
  [3,15],
  [20],
  [7]
]

Examples 2:

Examples 3:

Note

左右孩子的index分别是父节点index减一和加一

使用TreeMap是为了维持输出的顺序,Key: Column Index, Value: List

用一个column Queue去记录当前的column index

Code

Last updated