4. Level Order Traversal

Given the root of a binary tree, return the level order traversal of its nodes' values. (i.e., from left to right, level by level).

Example 1:

Input : root = [3, 9, 20, null, null, 15, 7]

Output : [ [3] , [9, 20] , [15, 7] ]

Explanation :

Example 2:

Input : root = [1, 4, null, 4 2]

Output : [ [1] , [4] , [4, 2] ]

Explanation :

Now Your Turn!

Pick the correct output for the given input

Input : root = [5, 1, 2, 8, null, 4, 5, null, 6]

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 0 <= Number of Nodes <= 2000
  • -1000 <= Node.val <= 2000

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

Fun Facts

0
/**
* Definition for a binary tree node.
* struct TreeNode {
* int data;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int val) : data(val) , left(nullptr) , right(nullptr) {}
* };
**/
 
class Solution {
public:
vector<vector<int> > levelOrder(TreeNode* root) {
//your code goes here
}
};
Test Case

Input:

Root