Binary Tree Level Order Traversal II

Navneet Ojha
1 min readJul 3, 2020

--

LeetCode July Challenge [Week 1]

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

For example:
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]
]

Solution:

We have to maintain a list of levels to solve this problem. Also, we need to maintain a number of nodes at the current level and next level so as to store it in results. We will be using the breadth-first search algorithm to solve this.

This is level order traversal and we need a queue to traverse.

Happy Coding !!!

--

--

Navneet Ojha
Navneet Ojha

Written by Navneet Ojha

I am Indian by birth, Punjabi by destiny. Humanity is my religion. Love to eat, travel, read books and my million dreams keep me alive.

No responses yet