Zigzag level order
Mediumtreebfsqueue
Like level order, but the direction alternates: the first level left to right, the second right to left, and so on.
Examples write a tree level by level, with null for a missing child: [3,9,20,null,null,15,7].
Example 1
Input: root = [3,9,20,null,null,15,7] Output: [[3],[20,9],[15,7]]
Example 2
Input: root = [1,2,3,4,5,6,7] Output: [[1],[3,2],[4,5,6,7]]
Constraints
- 0 ≤ number of nodes ≤ 2000
- -100 ≤ Node.val ≤ 100
Submit checks your solution against 7 tests, including hidden ones.
// Provided by the judge: keep these definitions
class TreeNode {
constructor(val = 0, left = null, right = null) {
this.val = val
this.left = left
this.right = right
}
}
/**
* @param {TreeNode|null} root
* @return {int[][]}
*/
function zigzagLevelOrder(root) {
}