Solve Leetcode Problems and Get Offers From Your Dream Companies||Binary Tree Level Order Traversal
Tags: algorithms, code, leetcode, programming
Published: 2021-03-19 12:54:24


## Solve Leetcode Problems and Get Offers From Your Dream Companies

Problem 102. Binary Tree Level Order Traversal

![](https://cdn-images-1.medium.com/max/2560/1*m-W1cVpcxvqtL7_l1RnTZA.png)

In this series, I am going to solve Leetcode medium problems live with my friends, which you can see on our youtube channel, Today we will do Problem Leetcode: 102. Binary Tree Level Order Traversal.

A little bit about me, I have offers from **Uber** **India** and **Amazon** **India** in the past, and I am currently working for **Booking.com** in Amsterdam.

## Motivation to learn algorithms
[**Solve Leetcode Problems and Get Offers From Your Dream Companies**](https://medium.com/leetcode-simplified/solve-leetcode-problems-and-get-offers-from-your-dream-companies-2786415be0b7)

## Problem Statement

Given the root of a [binary tree](https://medium.com/javarevisited/20-binary-tree-algorithms-problems-from-coding-interviews-c5e5a384df30), return the level order traversal of its nodes’ values. (i.e., from left to right, level by level).

### Example 1:

![](https://cdn-images-1.medium.com/max/2000/1*p9YwcMXN3kJrIZUbHY7zjg.png)

    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:**

* The number of nodes in the tree is in the range [0, 2000].

* -1000 <= Node.val <= 1000

## Youtube Discussion
{% youtube NEDZft8ceQo %}
## Solution

This is one of the standard [coding problems on Binary Tree](https://www.java67.com/2020/02/top-40-binary-tree-interview-questions.html), in this problem we have to return the level order traversal of its nodes' values. (i.e., from left to right, level by level). This can be done by using BFS on the tree with help of a queue. For each node, first, the node is visited and then its child nodes are put in a FIFO queue.

 1. Create an empty queue q

 2. temp_node = root

 3. Loop while temp_node is not NULL
> # a. print temp_node->data.
> # b. Enqueue temp_node’s children(from left to right)
> # c. Dequeue a node from q.
>  The following code implements this algorithm

You can learn more about BFS in this tutorial
[**How to use Breath Fast Search Pattern for craking coding Interviews**](https://medium.com/javarevisited/how-to-use-breath-fast-search-pattern-for-craking-coding-interviews-50ca7e827199)

## C++ Solution
{% gist https://gist.github.com/tailorsmit/e5c069ac810a91711cb7c2a69cc00d32.js %}
## **Java Solution**
{% gist https://gist.github.com/tailorsmit/79dbfbe938a4bd648fac85ad168d6ce7.js %}
## Complexity Analysis

**The time complexity is O(n)**

**The space complexity is O(n)**

## Thank You for reading and Follow this publication for more LeetCode problems!😃
[**LeetCode Simplified**](https://medium.com/leetcode-simplified)
[**50+ Data Structure and Algorithms Interview Questions for Programmers**](https://medium.com/hackernoon/50-data-structure-and-algorithms-interview-questions-for-programmers-b4b1ac61f5b0)