L10. iterative Inorder Traversal in Binary Tree | C++ | Java | Stack

  Рет қаралды 337,426

take U forward

take U forward

Күн бұрын

Пікірлер: 168
@peacejoy5293
@peacejoy5293 Жыл бұрын
whatever you teach just goes straight into my head , thanks for making me feel not dumb.
@pushkarraja4938
@pushkarraja4938 2 жыл бұрын
I bought a course from Coding Ninjas, I spent about 6000 on that and they have not even talked about iterative ways in the tree part. Thanks Striver bhai for making these high quality videos for free.
@joebale9670
@joebale9670 2 жыл бұрын
haan sahi baat hai wo lavde paise khane bethe hain mc.
@vinayjangra1401
@vinayjangra1401 2 жыл бұрын
😅i am also from there! btw no one is prefect, jha se gyan mile lelo🙃
@abhigyanraha5620
@abhigyanraha5620 2 жыл бұрын
Striver is absolutely insane. He is doing god's work IMO. The production quality, the clarity of concepts, the length of the videos, the depth of explanation and the concept coverage is absolutely mind-blowing. Techdose is another channel that explains every question in depth!
@lifehustlers164
@lifehustlers164 Жыл бұрын
mujhe offer mein 4100 ka milgya tha but still trees mein confidence nhi aaya. But ig maine trees thoda rush kia tha ,islie mujhe nhi aaya ache se samjh , course to acha hai vo no doubt, it all depends on the efforts ig in the end !!! Well i am glad you also have bought it,where are u now?
@anubhaaav
@anubhaaav 4 ай бұрын
@@lifehustlers164 where are you now?
@theSeniorSDE
@theSeniorSDE 3 жыл бұрын
Striver, Day 2 : completed this as well.
@deepaksarvepalli2344
@deepaksarvepalli2344 3 жыл бұрын
Felt bad... your work deserves more likes ...those who watched it, please like this..so that it will improves sir's interest
@thandepaji
@thandepaji 2 жыл бұрын
@Rohit Vathumilli kha intern/placement lgi bhai
@himanshu5549
@himanshu5549 Жыл бұрын
vector inorder(Node *root) { Node *curr = root; stack s; vector ans; while (true) { if (curr != NULL){ s.push(curr); curr = curr->left; } else { if (s.empty()) break; curr = s.top(); s.pop(); ans.push_back(curr->data); curr = curr->right; } } return ans; }
@nileshsinha7869
@nileshsinha7869 3 жыл бұрын
Please never stop making such series. Loving it❤
@manishjha6946
@manishjha6946 2 жыл бұрын
I don't know weather he is gifted, genius or really really hardworker. Anyways one thing is sure is a giver. Thanks Raj.
@shivangisrivastava1158
@shivangisrivastava1158 3 жыл бұрын
Very clearly explained! 👏 Just one thing, we can use root variable itself, instead of declaring 'node'. It still works! ( however no memory usage improvement on leetcode)
@_theCorporateMonk
@_theCorporateMonk 3 жыл бұрын
Exactly!
@Cool96267
@Cool96267 3 жыл бұрын
In that case we'll lose the root of tree, which we may require for further work.
@saketmehta6697
@saketmehta6697 Жыл бұрын
Modification of input ds is not at all recommended in interviews or real production env.
@IBMX65
@IBMX65 14 күн бұрын
@@_theCorporateMonk not a good practice brother
@PrashantSingh-jy6zp
@PrashantSingh-jy6zp 3 жыл бұрын
for skip promotion go to 0:43 code explanation at 8:49
@shubamgoswami
@shubamgoswami 3 жыл бұрын
most imp
@ishwaripednekar5164
@ishwaripednekar5164 10 ай бұрын
Hello Striver, You explain code very well. Thankyou for such kind of content.
@sangharshpipare666
@sangharshpipare666 8 ай бұрын
Thanks for making concepts easy to understand
@lifehustlers164
@lifehustlers164 Жыл бұрын
Completed 11/54(20% done) !!!
@AyushMishra-b8w
@AyushMishra-b8w 7 ай бұрын
Everything is very much clear to me thanks a lot bro
@allinonep.9454
@allinonep.9454 2 жыл бұрын
I first like the video then starts watching 🤩....! Because I know your videos are always amazing. Thank you so much for such a amazing series.
@adityajain5101
@adityajain5101 2 жыл бұрын
A gentle feedback : The explanation is Great but here you could have given more light on the statement that how we are making iterative solution from recursion inorder solution discussed before ..... you have told that this is taken from recursive but with more explanation I think But Thankyou for the amazing series
@chiraggaba_030
@chiraggaba_030 11 ай бұрын
Love You Man..., You are the Lord for Techies.
@tanujamaurya8875
@tanujamaurya8875 11 ай бұрын
Thank you Striver Bhaiya! for your efforts to make such great content !
@sukritinarang5016
@sukritinarang5016 2 жыл бұрын
Recursive and Iterative both approaches are clear !Thanks
@supriyakeshri78
@supriyakeshri78 3 күн бұрын
please start string series also.It will be very helpful
@apmotivationakashparmar722
@apmotivationakashparmar722 Ай бұрын
Thank you So much . Please Upload video Solutions of left Questions.
@ganeshjaggineni4097
@ganeshjaggineni4097 4 ай бұрын
NICE SUPER EXCELLENT MOTIVATED
@shivalikagupta3433
@shivalikagupta3433 2 жыл бұрын
Thank you soo much !!! Recursive and Iterative clearing both the approaches. :))))
@vineetjadhav1785
@vineetjadhav1785 3 ай бұрын
Very Good Explaination❤❤🔥🔥
@gaikwadpratik5872
@gaikwadpratik5872 Жыл бұрын
Thank you striver bhai. I think your way of teaching and delivering the concept is awesome. For me you are not lesser than GOD. I really don't have words to explain my feelings. @striver
@prabhakaran5542
@prabhakaran5542 4 ай бұрын
Understood ❤
@carefree_ladka
@carefree_ladka Ай бұрын
Hey man , good work . However I wanna point out that you don't need to run your while loop infinitely and break it . We can simplify it as below: function inOrderTraversal(root) { const stack = []; const result = []; while (root || stack.length) { // Go as left as possible and push all left nodes to the stack while (root) { stack.push(root); root = root.left; } // Backtrack by popping from the stack root = stack.pop(); result.push(root.val); // Add the node value to the result // Now, visit the right subtree root = root.right; } return result; }
@sahilmathur7371
@sahilmathur7371 2 жыл бұрын
Hello Striver, You explain code very well. Thankyou for such kind of content.
@samuelfrank1369
@samuelfrank1369 Жыл бұрын
UNDERSTOOD. THANKS A LOT.
@bhavkushwaha
@bhavkushwaha 7 ай бұрын
Thankyou Striver, Understood!
@tps8470
@tps8470 4 ай бұрын
Easy approach
@fantasyillusion
@fantasyillusion 2 жыл бұрын
Those who are looking for python solution # 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 inorderTraversal(self, root: Optional[TreeNode]) -> List[int]: inorder=[] node = root stack = [] while True: if node!=None: stack.append(node) node=node.left else: if len(stack)==0: break node=stack.pop() inorder.append(node.val) node=node.right return inorder
@mannmehta4841
@mannmehta4841 Ай бұрын
Without infinite look condition, but O(N) extra space to track we have visited a child node or not - Code: void inordere_traversal_interative(Node* root) { stack st; st.push(root); unordered_map vis; while(!st.empty()) { Node* node = st.top(); if (node->left != NULL and !vis[node->left]) { st.push(node->left); continue; } coutright); continue; } } }
@sanjana8267
@sanjana8267 2 жыл бұрын
We love your series! And I can not thank you enough for your efforts!
@abhaytiwari6411
@abhaytiwari6411 3 жыл бұрын
thanks, bro for making such a wonderful series
@UECAshutoshKumar
@UECAshutoshKumar Жыл бұрын
Thank you sir
@sonalisingh7932
@sonalisingh7932 2 ай бұрын
understood👍👍
@RahulKumar-rk1tf
@RahulKumar-rk1tf 2 жыл бұрын
Start: 0:43
@coding8000
@coding8000 Жыл бұрын
understooooooooood! Thanks again
@jambajuice07
@jambajuice07 Жыл бұрын
striver is not a just a emotion . it is a name !
@jasonbrody4618
@jasonbrody4618 3 жыл бұрын
Liked. Cfbr will watch after some time
@nawabkhan4916
@nawabkhan4916 2 жыл бұрын
great work, and have lots of sponsor ad so that you can provide great videos.
@studyaccount794
@studyaccount794 2 жыл бұрын
You're doing god's work here man don't stop.
@1tav0
@1tav0 2 жыл бұрын
Thank you so much striver i wish i had friends like you in my life
@aayushtheapple
@aayushtheapple 6 ай бұрын
Alternative : ```cpp void inorder(Node* root) { if (root == NULL) return; stack st; Node* current = root; while (current != NULL || !st.empty()) { while (current != NULL) { st.push(current); current = current->left; } current = st.top(); st.pop(); cout data right; } } ```
@DeadPoolx1712
@DeadPoolx1712 Ай бұрын
UNDERSTOOD;
@culeforever5408
@culeforever5408 Жыл бұрын
understood
@armangaming8666
@armangaming8666 2 жыл бұрын
really bohot amazing hai ye free ka tree series
@parasgupta1391
@parasgupta1391 4 ай бұрын
last mei root = node->right hoga naaki node = node->right
@uRamPlus
@uRamPlus 3 жыл бұрын
just discovered your channel! thank you so much for your effort! please dont stop making these tutorials! just subbed
@vedanshsharma1156
@vedanshsharma1156 Жыл бұрын
bhai phle hi stack empty hojayega 7 k left null par 7 jayega or right null par 1 jayega
@kasyapdharanikota8570
@kasyapdharanikota8570 3 жыл бұрын
thank you very much please continue making such great videos
@mriduljain1981
@mriduljain1981 Жыл бұрын
completed lecture 10 of tree playlist.
@Learnprogramming-q7f
@Learnprogramming-q7f 8 ай бұрын
Thank you Bhaiya
@ashutoshchaudhary100
@ashutoshchaudhary100 11 ай бұрын
Wooowwww🎉
@mohammadumaidansari5087
@mohammadumaidansari5087 4 ай бұрын
Understood
@vakhariyajay2224
@vakhariyajay2224 Жыл бұрын
Thank you very much. You are a genius.
@project_eth
@project_eth 4 ай бұрын
*understood*, day2
@cinime
@cinime 2 жыл бұрын
Understood! Super cool explanation as always, thank you very much!!
@vegitogamingpubg3364
@vegitogamingpubg3364 3 жыл бұрын
Thank you for this much effort and hard work.. ❤
@shikharagrawal2208
@shikharagrawal2208 Жыл бұрын
Thank u so much striver for such great Playlist
@ujjwalsharmaiiitunacse3884
@ujjwalsharmaiiitunacse3884 Жыл бұрын
dhanetawasi code :- vector inOrder(Node* root) { stack st; vector ans; if(root!=NULL) st.push(root); while(!st.empty()) { if(root==NULL) { st.pop(); if(st.empty()) break; root =st.top();st.pop(); ans.push_back(root->data); //right wali ko dalinge root=root->right; st.push(root); continue; } st.push(root->left); root=root->left; } return ans; }
@pavankumar-lv4ne
@pavankumar-lv4ne 3 жыл бұрын
best coding youtuber ever
@namansharma5128
@namansharma5128 3 жыл бұрын
🚨🙋‍♂️📢Doubt- What is that while loop doing at line 19, we are not giving any condition, we have just written true? What does it signify?
@KajalKumari-wy6ml
@KajalKumari-wy6ml 2 жыл бұрын
It shows while( !stack.empty() || node != null)
@chakshudeepmeharwal3547
@chakshudeepmeharwal3547 2 жыл бұрын
Line 26 has it
@curs3m4rk
@curs3m4rk 3 жыл бұрын
finally understood this! much thanks to you
@ideepakpandey
@ideepakpandey 2 жыл бұрын
thank u so much for this series.....
@codemarshal655
@codemarshal655 Жыл бұрын
Did you know, you can do iterative in / pre / post order traversals using same code?? Checkout how I did it in just 20 lines, in my new video kzbin.info/www/bejne/bKjbf5ZunKidbqc
@harshitjaiswal9439
@harshitjaiswal9439 9 ай бұрын
understood.
@rupalikumari8829
@rupalikumari8829 5 ай бұрын
Understood : )
@Mr.Indianwxvhehsy9191hghgx
@Mr.Indianwxvhehsy9191hghgx 11 ай бұрын
i like it
@pankajsunal9819
@pankajsunal9819 6 ай бұрын
Understood .07/05/2024
@uplsove
@uplsove 2 жыл бұрын
This is for people who code in python def inorderTraversal(self, root: Optional[TreeNode]) -> List[int]: stack = [] inorder = [] curr = root while True: if curr!=None: stack.append(curr) curr = curr.left else: if len(stack)==0: break curr = stack.pop() inorder.append(curr.val) curr = curr.right return inorder
@lovetocode9266
@lovetocode9266 3 жыл бұрын
Is there any optimization while we are using iterative approach. As in iterative approach also we have time complexity of O(n) and aux.space O(n) . Isn't it same if we are using recurrive approach?
@takeUforward
@takeUforward 3 жыл бұрын
Yeah its just for interviews.
@Cool96267
@Cool96267 3 жыл бұрын
I don't think there can be any more optimization of time, as the name itself says 'traverse' we need to traverse whole of the n nodes.
@bharadwajsaibandaru4323
@bharadwajsaibandaru4323 2 жыл бұрын
There's actually something called Morris traversal , only for inorder . You can check that out on GFG . It's a bit advanced I guess. It doesn't use recursion and stack even.
@anuraj8979
@anuraj8979 Жыл бұрын
I understood concept...is any necessary to mugup code?
@jaydalsaniya6986
@jaydalsaniya6986 3 жыл бұрын
Press L four times at the starting of the video .
@namansharma5128
@namansharma5128 3 жыл бұрын
I would have to press it 8 times .......
@_hulk748
@_hulk748 Жыл бұрын
Understood sir❤🙏🙇‍♂
@abhishekjakhmola1434
@abhishekjakhmola1434 Жыл бұрын
amazing explaination
@lavanyaprakashjampana933
@lavanyaprakashjampana933 2 жыл бұрын
we love your content and we love you....🖤
@_AkashRuidas
@_AkashRuidas 2 жыл бұрын
Great Explaination
@jeetpatel7879
@jeetpatel7879 3 жыл бұрын
while(true){ if(node!= NULL){ st.push(node); node = node->left; } else{ if(st.empty()== true) break; node = st.top(); ans.push_back(node->val); st.pop(); node= node->right; } } sir isme ye while(true) konsi condition cheak karta hei?? I mean when its going to be false?
@patrickhudson5168
@patrickhudson5168 3 жыл бұрын
With while(true), the loop will be running infinitely. But here, we have written a condition that when stack becomes empty, the loop will break.
@jeetpatel7879
@jeetpatel7879 3 жыл бұрын
@@patrickhudson5168 then how else condition will run??? You say is st.empty()==true then loop break then in loop how else condition will run??
@shivangisrivastava1158
@shivangisrivastava1158 3 жыл бұрын
@@jeetpatel7879 Note that break statement is inside an "if" condition, so it runs only when stack is empty, other times else block will get executed.
@jeetpatel7879
@jeetpatel7879 3 жыл бұрын
@@shivangisrivastava1158 when st.empty()==true then loop break then in loop how else condition will run? I checked that and also done dry run of code and the conclusion is when node!=NULL || !st.empty() this both condition is false then and only then loop will terminate
@prabhagaikwad4849
@prabhagaikwad4849 Жыл бұрын
Below code is more cleaner, you may check once, public IList InorderTraversal(TreeNode root) { IList list = new List(); Stack stack = new Stack(); TreeNode node = root; while (node != null || stack.Count > 0) { if (node != null) { stack.Push(node); node = node.left; } else { node = stack.Pop(); list.Add(node.val); node = node.right; } } return list; }
@rahulchandran6482
@rahulchandran6482 Жыл бұрын
Very helpful
@sagarchauhan3187
@sagarchauhan3187 3 жыл бұрын
Hello Sir, Can you please complete the remaining videos of trees and graph of SDE sheet? please please upload!!🥺🥺
@programmertik2046
@programmertik2046 2 жыл бұрын
Here are the iterative traversals with explaination from me //Iterative traversals DFS You need to change only one line recursion simuated !! SC:O(2N) void InOrderIter(TreeNode *root){ unordered_mapstateMap; stacknodes; //push the root first nodes.push(root); while(!nodes.empty()){ TreeNode *currNode=nodes.top(); //if currNode is null simply pop it out if(currNode==NULL) { nodes.pop(); continue; } //if node is not present then initialize it in map //you may or may not do this as the map by default initializes //if key doesnt exist // if(stateMap.find(currNode)==stateMap.end()){ // stateMap[currNode]=0; // } //the nodes left side is not processed yet so process it if state of that node is 0 if(stateMap[currNode]==0){ nodes.push(currNode->leftPtr); } //the nodes left side is processed so process the root now and print its data it if state of that node is 1 else if(stateMap[currNode]==1){ cout
@nagavedareddy5891
@nagavedareddy5891 2 жыл бұрын
Huge Respect...❤👏
@sohamdachawar8354
@sohamdachawar8354 11 ай бұрын
what si the while condition as in the code it is mentioned only true. but what is that true condition when should the loop stop working...I am unable to frame it in the condition.
@RahulKumar-nd2sp
@RahulKumar-nd2sp 5 ай бұрын
Same doubt
@II-ii2um
@II-ii2um 3 жыл бұрын
What software are you using to draw these explanations man? Really nice colours you have there. Makes it light on the eyes.
@shreyasnagabhushan4918
@shreyasnagabhushan4918 2 жыл бұрын
understood sir
@ashitasrivastava681
@ashitasrivastava681 Жыл бұрын
UNDERSTOOD!
@helloworldkhalid
@helloworldkhalid 3 жыл бұрын
very nice sir thankyou so much
@chandrachurmukherjeejucse5816
@chandrachurmukherjeejucse5816 Жыл бұрын
Understood.
@somyasrivastava6315
@somyasrivastava6315 2 жыл бұрын
Just Beautiful
@kushagramishra5638
@kushagramishra5638 Жыл бұрын
underStood!
@akshatchaube1213
@akshatchaube1213 Жыл бұрын
L 10 Done
@madhurgupta9145
@madhurgupta9145 3 жыл бұрын
Keep up the good work
@t-anime517
@t-anime517 2 жыл бұрын
Understood 😊
@rudranshvyas7539
@rudranshvyas7539 3 жыл бұрын
In c++ code what does "TreeNode *node = root;" is actually doing?? at 9:01
@YashPandey_the_emperor
@YashPandey_the_emperor 3 жыл бұрын
It's just a Node iterator.
@utkarshsharma6650
@utkarshsharma6650 2 жыл бұрын
understooood. thanks :)
@ajayypalsingh
@ajayypalsingh 2 жыл бұрын
💚
@as_a_tester
@as_a_tester 2 жыл бұрын
tysm striver bhai
@enigma2777
@enigma2777 2 жыл бұрын
Understood 🖤
@shivanisinha7035
@shivanisinha7035 2 жыл бұрын
Great content
This Game Is Wild...
00:19
MrBeast
Рет қаралды 152 МЛН
L14. Maximum Depth in Binary Tree | Height of Binary Tree | C++ | Java
8:05
L9. Iterative Preorder Traversal in Binary Tree | C++ | Java | Stack
6:50
Fastest Way to Learn ANY Programming Language: 80-20 rule
8:24
Sahil & Sarra
Рет қаралды 915 М.
8 patterns to solve 80% Leetcode problems
7:30
Sahil & Sarra
Рет қаралды 438 М.