Binary Tree Level Order Traversal
Given the root of a binary tree, return its values level by level: an array of arrays where the first inner array holds the root's value, the second holds the values one level down, and so on. Within a level, list the values from left to right.
Return an empty array for an empty tree.
Constraints
- The tree has between
0and2000nodes. -1000 <= Node.val <= 1000
Examples
Example 1
Input: root = [1,2,3,null,4,5,6,7] Output: [[1],[2,3],[4,5,6],[7]]
Row by row: [1], then [2, 3], then 4 (under 2) followed by 5 and 6 (under 3), then 7 (under 4).
Example 2
Input: root = [8] Output: [[8]]
One node, one level.
Example 3
Input: root = [] Output: []
No nodes, so no levels: return an empty array.
JavaScript you'll use: Stacks, queues and deques Arrays: create, read, update
/**
* Tree nodes are provided (you don't need to define them):
* class TreeNode { constructor(val = 0, left = null, right = null) { this.val = val; this.left = left; this.right = right; } }
*/
/**
* @param {TreeNode | null} root
* @return {number[][]}
*/
function levelOrder(root) {
}
Ctrl/⌘ + ' runs the examples, Ctrl/⌘ + Enter submits. Use console.log freely: output shows under Console. In the editor, press Esc then Tab to move focus out.