314. Binary Tree Vertical Order Traversal

Medium

2131

248

Add to List

Share

Given the root of a binary tree, return the vertical order traversal of its nodes' values. (i.e., 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 1:

https://assets.leetcode.com/uploads/2021/01/28/vtree1.jpg

Input: root = [3,9,20,null,null,15,7]
Output: [[9],[3,15],[20],[7]]

Example 2:

https://assets.leetcode.com/uploads/2021/01/28/vtree2-1.jpg

Input: root = [3,9,8,4,0,1,7]
Output: [[4],[9],[3,0,1],[8],[7]]

Example 3:

https://assets.leetcode.com/uploads/2021/01/28/vtree2.jpg

Input: root = [3,9,8,4,0,1,7,null,null,null,2,5]
Output: [[4],[9,5],[3,0,1],[8,2],[7]]

Constraints: