Reverse Odd Levels of Binary Tree | Detailed | DFS | BFS | Leetcode 2415 | codestorywithMIK

preview_player
Показать описание
This is the 53rd Video of our Playlist "Binary Tree : Popular Interview Problems" by codestorywithMIK

In this video we will try to solve normal Binary Tree problem : Reverse Odd Levels of Binary Tree | DFS | BFS | Leetcode 2415 | codestorywithMIK

I will explain the intuition so easily that you will never forget and start seeing this as cakewalk EASYYY.
We will do live coding after explanation and see if we are able to pass all the test cases.
Also, please note that my Github solution link below contains both C++ as well as JAVA code.

Problem Name : Reverse Odd Levels of Binary Tree | DFS | BFS | Leetcode 2415 | codestorywithMIK
Company Tags : will update later

╔═╦╗╔╦╗╔═╦═╦╦╦╦╗╔═╗
║╚╣║║║╚╣╚╣╔╣╔╣║╚╣═╣
╠╗║╚╝║║╠╗║╚╣║║║║║═╣
╚═╩══╩═╩═╩═╩╝╚╩═╩═╝

Summary :
Approach 1: Simple BFS

Idea: Perform a level-order traversal (BFS) using a queue to process nodes level by level. At each odd level, reverse the values of the nodes.
Steps:
Use a queue to store nodes of the current level.
Collect nodes at each level into a list.
If the level is odd, swap values between nodes from opposite ends of the list.
Approach 2: DFS(Recursion Leap Of Faith)

Idea: Use a recursive DFS to traverse the tree simultaneously from the leftmost and rightmost nodes of each level. At odd levels, swap the values of the paired nodes.
Steps:
Recursively process left and right subtrees in a mirrored fashion.
Swap values of nodes at odd levels.

✨ Timelines✨
00:00 - Introduction

#MIK #mik #Mik
#coding #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge #leetcodequestions #leetcodechallenge #hindi #india #coding #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge #leetcodequestions #leetcodechallenge #hindi #india #hindiexplanation #hindiexplained #easyexplaination #interview#interviewtips #interviewpreparation #interview_ds_algo #hinglish #github #design #data #google #video #instagram #facebook #leetcode #computerscience #leetcodesolutions #leetcodequestionandanswers #code #learning #dsalgo #dsa #coding #programming #100daysofcode #developers #techjobs #datastructures #algorithms #webdevelopment #softwareengineering #computerscience #pythoncoding #codinglife #coderlife #javascript #datascience #leetcode #leetcodesolutions #leetcodedailychallenge #codinginterview #interviewprep #technicalinterview #interviewtips #interviewquestions #codingchallenges #interviewready #dsa #hindi #india #hindicoding #hindiprogramming #hindiexplanation #hindidevelopers #hinditech #hindilearning #helpajobseeker #jobseekers #jobsearchtips #careergoals #careerdevelopment #jobhunt #jobinterview #github #designthinking #learningtogether #growthmindset #digitalcontent #techcontent #socialmediagrowth #contentcreation #instagramreels #videomarketing #codestorywithmik #codestorywithmick #codestorywithmikc #codestorywitmik #codestorywthmik #codstorywithmik #codestorywihmik #codestorywithmiik #codeistorywithmik #codestorywithmk #codestorywitmick #codestorymik #codestorwithmik
Рекомендации по теме
Комментарии
Автор

Qualified tcs codevita.. Rank 3066
Thank you so much mik.. You made graph so easy... I've tried learning graphs from many but the way you teach is unmatchable... Hats off man❤... Thank you so much for providing such content for free... I've been following your channel for 5-6 months and didn't miss a day. Even though sometimes I solve it myself but I come here only for approach.
Anyone who's reading this... Just be consistent man! You'll think that nothing is happening but eventually you'll see the results. And don't do it just for the sake of job, focus on enjoying the process that's more important. Develop curiosity.
Eventually your time complexity will go from O(n) to O(logn) to O(1) 😂

Booksmadesimple
Автор

I came here to understand DFS only and it was crystal clear. I also paused the video and solved Symmetric Tree on my own.

gui-codes
Автор

Already solved it using your first approach ❤

jeehub
Автор

What a beautiful explanation of dfs approach

time_square
Автор

Hi Mik,
The DFS approach was A1. If we think about it, we're basically employing a 2-pointer approach along with recursion. Even though it's simple, it didn't come to me naturally at first. I just started solving problems on trees, so from my POV, the fact that you were able to notice that was simply amazing. Keep up the great work!

shekmazed
Автор

one of the best explanation of dfs approach.

satyasanjay
Автор

Impressive approach and teaching man, I absolutely love the effort you put into this and your skill!

abhinaysahuu
Автор

mik sir you have such great teaching skill

Be_zanshin
Автор

Thank you so much mik, got rank of around 500 in tcs codevita, your videos are so helpful and qualifed for round 2.

vickyroy
Автор

Your explanation always make it easy to understand. One day, I want to think like you when approaching a problem, bhaiya. Thank you for sharing :-)

ashwin_anand_dev
Автор

I got selected for round 2 TCS CodeVita with a rank of 6166. I only solved one question, which took me 30-40 minutes, as I was busy the entire day due to some personal matters. I was initially worried about whether I would get selected since I couldn't dedicate more time, but fortunately, I got selected. Thank you so much, sir! I am very grateful to have a teacher like you.

Vibhanshushrivastava
Автор

I started watching the video & at 2:55, something sparked in my mind, at 3:50 I left the video & started coding it by myself. And indeed yes, a successful submission.

class Solution {
public:
TreeNode* reverseOddLevels(TreeNode* root) {

queue<TreeNode*> q;
q.push(root);
int i = 0;
while (!q.empty()) {
int n = q.size();
vector<TreeNode*> nodes;
vector<int> vals;
while (n--) {
TreeNode* node = q.front();
if (i % 2) {
nodes.push_back(node);
vals.push_back(node->val);
}
if (node->left != NULL) {
q.push(node->left);
q.push(node->right);
}
q.pop();
}
n = nodes.size();
for (int x = 0; x < n; x++) {
nodes[x]->val = vals[n - 1 - x];
}
i++;
}

return root;
}
};

anonymoushackerar
Автор

👍👍

instead of using queue and vector
using DEQUE in C++
class Solution {
public:
TreeNode* reverseOddLevels(TreeNode* root) {
deque<TreeNode*> d;
d.push_back(root);
bool odd = true;
while(!d.empty()){
int size=d.size();
TreeNode* temp;
for(int i=0;i<size;i++){
temp=d.front();
d.pop_front();
if(temp->left) d.push_back(temp->left);
if(temp->right) d.push_back(temp->right);
}
if(odd){
int i=0, j=d.size()-1;
while(i<j){
swap(d[i++]->val, d[j--]->val);
}
}
odd=!odd;
}
return root;
}
};

ghoshalsingh
Автор

Qualified for TCS Codevita Round 2 Rank - 2461🎉🎉
Would give all the credits to MIK sir ....following since one year and without sir It would not have been possible graph concepts playlist 🤌🤌 a lot ...🫂🫂

harjotanand
Автор

Mazhar bhai... Rishav here brother. 10th A Vcs... ❤️

nerdycuber
Автор

// BFS approach:

class Solution {
public:
TreeNode* reverseOddLevels(TreeNode* root) {
queue<TreeNode*> q;
q.push(root);
int level = 0;
while (!q.empty()) {
vector<TreeNode*> v;
int n = q.size();
while (n--) {
TreeNode* node = q.front();
if (node->left) q.push(node->left);
if (node->right) q.push(node->right);
q.pop();
v.push_back(node);
}
if (level % 2 == 1) {
int i = 0, j = v.size() - 1;
while (i < j) {
swap(v[i]->val, v[j]->val);
i++, j--;
}
}
level++;
}
return root;
}
};

Ranjan-mdnu
Автор

Invert BT + Symmetric tree + Level Order Traversal

bhaskar_saini
Автор

Just emphasizing once more.... the DFS recursive approach is working in this problem only because it's given that the in the input given is a perfect binary tree. ✨

swarnarupbhunia
Автор

Josh Technology group ke OA me similar Problem aaya tha Bus Odd ki jagah even tha
ctc:13LPA

robot.
Автор

bhiya codeforces contest ka solution bhi discuss kiya kro please 🙏🙏🙏

VarunSahu-tvrj
welcome to shbcf.ru