L8. Level Order Traversal of Binary Tree | BFS | C++ | Java

preview_player
Показать описание

Find DSA, LLD, OOPs, Core Subjects, 1000+ Premium Questions company wise, Aptitude, SQL, AI doubt support and many other features that will help you to stay focussed inside one platform under one affordable subscription. Have a hassle free one stop solution for up-skilling and preparing.

Checkout the problem link 👇🏼
Level Order Traversal of Binary Tree | BFS | C++ | Java

Рекомендации по теме
Комментарии
Автор

Hi, hope you are well.


Find DSA, LLD, OOPs, Core Subjects, 1000+ Premium Questions company wise, Aptitude, SQL and many other time saving features under one affordable subscription. Have a hassle free one stop solution for up-skilling and preparing yourself.


takeUforward
Автор

This series is a blessing for all those who find tree difficult...Please make series on every DS...Great Job!

dishikasingh
Автор

No one has ever made level order this easy!

PriyaGupta-sgsm
Автор

If you understand, like and :) Do follow me at Insta: striver_79

takeUforward
Автор

Done on 12 March 2025 at 10:59
Place : Study Room 2, Hostel 5, IIT Bombay

ErenYeager-dper
Автор

For those looking for python code

# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]:
ans=[]

if root==None:
return ans

queue=deque([root])

while queue:

n=len(queue)
level=[]

for i in range(n):

node=queue.popleft()

if node.left!=None: queue.append(node.left)
if node.right!=None: queue.append(node.right)

level.append(node.val)

ans.append(level)
return ans

fantasyillusion
Автор

Java soln using Recursion without any queue:

public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> ans = new ArrayList<>();
travel(0, root, ans);
return ans;
}

private void travel(int level, TreeNode cur, List<List<Integer>> ans) {
if (cur == null) return;
// add another list only when we visit a new level for the first time
if (level >= ans.size())
ans.add(new ArrayList<>());

ans.get(level).add(cur.val); // get the list of that level add the node val to it

travel(level + 1, cur.left, ans);
travel(level + 1, cur.right, ans);
}

HomiePlaysYT
Автор

Just started it and loving it.. On point clear explanation.. No khich khich... No time waste.. Worth watching.. ♥️

krishnasudan
Автор

Recursive Solution using BFS:
vector<vector<int>> ans;
void help(TreeNode *root, int depth)
{
if(root==NULL)
{
return;
}
if(ans.size()==depth)
{

}

help(root->left, depth+1);
help(root->right, depth+1);
}
vector<vector<int>> levelOrder(TreeNode* root)
{
help(root, 0);
return ans;
}

divyanshmishra
Автор

what a fentastic explaination sir. simply superb

banothutharun
Автор

UNDERSTOOD THANK YOU
May God Bless You❤

RachanaDatta
Автор

The expaination was so good that even after 6months I was able to solve it without help. Thank you @Striver bhaiya

SB-hbkx
Автор

Striver you are a boon to us Tier 3 Students :')
Love you man!

vedantsharma
Автор

Understood! Super gorgeous explanation as always, thank you very much!!

cinime
Автор

Awesome explanation.
Please upload all videos by tomorrow, else after weekend u will get buzy with office :/

manavshah
Автор

vector<int> levelOrder(Node* node)
{
vector<int> ans;
queue<Node *>q;
q.push(node);
while(!q.empty()){
//solving in hard way
int size =q.size();
for(int i=0;i<size;i++){
Node *front = q.front();
q.pop();
if(front->left){
q.push(front->left);
}
if(front->right){
q.push(front->right);
}
ans.push_back(front->data);
}
}
return ans;
}

shake-her
Автор

Sir aap bahut bhadiya pdate ho thank u for making this series

RitikKumar-bkpj
Автор

Level Order Traversal Approach in C++:
int maxDepth(TreeNode* root)
{

if(root == NULL) return 0;
vector<vector<int>> answer;

queue<TreeNode* > q;
q.push(root);

while(!q.empty())
{
int size = q.size();
vector<int> level;
for(int i=0;i<size;i++)
{
TreeNode* currentNode = q.front();
q.pop();
if(currentNode->left != NULL) q.push(currentNode->left);
if(currentNode->right != NULL) q.push(currentNode->right);


}
answer.push_back(level);

}
return answer.size();
}

MrMaharaj
Автор

I want to be hard working like you. You really live up to your channel name. ✌️

averylazyandweirdhuman
Автор

Shouldn't be the Space Complexity : O(width) cause the queue can grow upto max nodes in a level ?

skstudy
welcome to shbcf.ru