Level order traversal and network delay
Given a binary tree, produce the level-wise traversal of its node values. That is, return the values of nodes grouped by each level, from left to right.
For instance, consider the binary tree represented as [5,3,8,null,null,7,10]
:
5 / \ 3 8 / \ 7 10
Your task is to return the values grouped by levels as follows:
[ [5], [3, 8], [7, 10] ]
// Example 1: Input: root = [1, 2, 3, 4, 5, null, 7] Output: [ [1], [2, 3], [4, 5, 7] ] // Example 2: Input: root = [10] Output: [ [10] ] // Example 3: Input: root = [] Output: []