314 Binary Tree Vertical Order Traversal
1. Question
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.
Examples:
Given binary tree
[3,9,20,null,null,15,7]
,return its vertical order traversal as:
Given binary tree
[3,9,8,4,0,1,7]
,return its vertical order traversal as:
Given binary tree
[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),return its vertical order traversal as:
2. Implementation
(1) BFS + HashMap
3. Time & Space Complexity
BFS + HashMap:时间复杂度O(n),n为node的总个数, 空间复杂度O(n)
Last updated