Let’s Get to the Level Order Traversal next.

Breadth-First Search or Level Order Traversal.

BFS is used to traverse the Tree by levels. The above tree will be traversed:

1 ->2->3->4->5->6

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

For example

Given binary tree [3,9,20,null,null,15,7] ,

3

/ \

9 20

/ \

15 7

return its level order traversal as:

[

[3],

[9,20],

[15,7]

]

Code

Algorithm👨‍🎓

Create a queue that is going to store the next to visit nodes. Initially, Insert the root of the tree into the queue. Run till the queue is not empty. For each node, you visit. Look whether there is a left or right node. If there is add it to the Queue. Whenever you are adding new elements to the queue that means you have entered a new level. So when you traverse out of each level add it to the result set. Return the result set🔚

Take Some time to understand this Code. You are going to use this concept in the following⏭ Problems. Before Each Problem think of how to apply this method to solve the program

Binary Tree Level Order Traversal II

Given a binary tree, return the bottom-up level order traversal of its nodes’ values. (ie, from left to right, level by level from leaf to root).

Given binary tree [3,9,20,null,null,15,7] ,

3

/ \

9 20

/ \

15 7

return its bottom-up level order traversal as:

[

[15,7],

[9,20],

[3]

]

How would you apply BFS?

Code

What’s Different?

Just Before Returning the Result. Reverse the result.

Binary Tree Zigzag Level Order Traversal

Given a binary tree, return the zigzag level order traversal of its nodes’ values. (ie, from left to right, then right to left for the next level and alternate between).

Given binary tree [3,9,20,null,null,15,7] ,

3

/ \

9 20

/ \

15 7

return its zigzag level order traversal as:

[

[3],

[20,9],

[15,7]

]

Hint : You have to apply the Previous 2 approaches together.

Code

Introduce a new variable called dir which is going to indicate the direction in which you have to add the row to the 2D List.

0 → Left to Right || 1 → Right to Left

If the Directions is from Right to Left that is 1. You have to Reverse the Current Row then add it to the 2D list. Else just add the List.

Right Side View of Binary Tree

Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom.

Example:

Input: [1,2,3,null,5,null,4]

Output: [1, 3, 4]

Explanation: 1 <---

/ \

2 3 <---

\ \

5 4 <--

How will you apply BFS for this problem?

Code

Explanation

Whenever we traverse the tree by BFS at each level. The Last Element will represent the Right Side of The Binary Tree.

the tree by at each level. The will represent the of The Tree. All you have to make sure is, After each Level, you have to reinitialize the right element.

Additional Question

Try Solving the Left Side of the Binary Tree and the Bottom Left Tree Element using BFS. Similar to the method discussed above.

Aggregate Functions

I will work on one Example of Maximum Element.

of Try Average, Minimum of Levels on your own.

Given the root of a binary tree, the level of its root is 1 , the level of its children is 2 , and so on.

Return the smallest level X such that the sum of all the values of nodes at level X is maximal.

Example 1:

Input: [1,7,0,7,-8,null,null]

Output: 2

Explanation:

Level 1 sum = 1.

Level 2 sum = 7 + 0 = 7.

Level 3 sum = 7 + -8 = -1.

So we return the level with the maximum sum which is level 2.

If you could come up with the Solution here. Then I have Succeeded❤

Code

This is fairly straightforward. From what you had learned before. You traverse level by level. At Each Level add the row values and compare them with maximum value.

If it is greater than max. Change max and store the level. Return the max_sum level