Level order traversal
Mediumtreebfsqueue
Return the node values level by level, top to bottom and left to right within each level: one array per level.
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],[9,20],[15,7]]
Example 2
Input: root = [1] Output: [[1]]
Example 3
Input: root = [] Output: []
Constraints
- 0 ≤ number of nodes ≤ 2000
- -1000 ≤ Node.val ≤ 1000
Submit checks your solution against 8 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 levelOrder(root) {
}