← 返回 meta 的题目列表Variant of BST Level Order Traversal
类型:online_judge
Given a binary search tree (BST), implement a variant of level order traversal. The requirement is to reverse the order of nodes in every alternate level. For example, the first level is from left to right, the second level is from right to left, the third level is left to right again, and so on.
Input Format
The root node of a binary search tree.
Output Format
A 2D array where each subarray represents the values of a level.
Sample Input
[3, 9, 20, null, null, 15, 7]
Sample Output
[[3], [20, 9], [15, 7]]
Constraints
Number of nodes range from [0, 2000].
Node values range from [-1000, 1000].
Example
Input
[3, 9, 20, null, null, 15, 7]