L9. Iterative Preorder Traversal in Binary Tree | C++ | Java | Stack

  Рет қаралды 335,882

take U forward

take U forward

Күн бұрын

Пікірлер: 183
@takeUforward
@takeUforward 3 жыл бұрын
Please likeeee, shareeee and subscribeeeeeeee :) Also follow me at Insta: Striver_79
@democratcobra
@democratcobra 3 жыл бұрын
Understood
@sarthakbhatia7888
@sarthakbhatia7888 3 жыл бұрын
done
@AbhishekKumar-vr7sh
@AbhishekKumar-vr7sh 3 жыл бұрын
Yesterday I gave my paytm interview and all the dsa questions asked were from the sde sheet. Moreover, the questions asked from the core subjects were from the core sheet. I was able to crack the interview with great ease all thanks to striver bhaiya OP. You are the best mentor in this world 🔥
@AbhishekKumar-vr7sh
@AbhishekKumar-vr7sh 3 жыл бұрын
@Himanshu Kataria 😂😂😂
@debanjan_poddar
@debanjan_poddar 3 жыл бұрын
You gave the interview yesterday.. How are you already sure that you will be selected?
@AbhishekKumar-vr7sh
@AbhishekKumar-vr7sh 3 жыл бұрын
@@debanjan_poddar It was an on campus interview. Results came out that night itself. Processing is faster in case of on campus placement.
@AbhishekKumar-vr7sh
@AbhishekKumar-vr7sh 3 жыл бұрын
@@aadeshsharma0001 thanks bro
@singhsahab9478
@singhsahab9478 3 жыл бұрын
@Himanshu Kataria lol
@111rhishishranjan2
@111rhishishranjan2 Жыл бұрын
vector Solution::preorderTraversal(TreeNode* A) { stackst; TreeNode*node =A; vectorv; while(!st.empty() || node != NULL){ if(node != NULL){ v.push_back(node->val); st.push(node); node = node->left; } else{ node = st.top(); st.pop(); node = node->right; } } return v; } //this is a nice stack approach . with more clean code.
@curs3m4rk
@curs3m4rk 3 жыл бұрын
watched a lot of videos for iterative traversal, but this one hit my brain in first attempt. Your explanation is great man
@aryanpinto5105
@aryanpinto5105 Жыл бұрын
Was just able to code on my own. The way you give the intuition is just next level. Tysm Striver. Grateful to have a teacher like you.❤💯
@piyushsaxena6243
@piyushsaxena6243 2 жыл бұрын
this is the best tree series till date , thanks a lot striver, would be really helpful if you could bring a playlist on dp on trees as well for both cp and interviews.
@shivangisrivastava1158
@shivangisrivastava1158 3 жыл бұрын
i am thrilled at my transformation, i was able to write correct code before looking at your code! Tysm bhaiya😵♥️
@harshvardhanmishra1072
@harshvardhanmishra1072 2 жыл бұрын
Started this tree series today and I don't know when I come up to the Lecture number 9. Such a wonderful explanation. Thanks you so much Sir for doing so much effort for us.😊
@Ishan301
@Ishan301 3 жыл бұрын
Striver is life saviour for tier-3 college guys like me. Thank you.
@Codeology708
@Codeology708 3 жыл бұрын
This Man Is a great example for those who say you can get a placement in Giant MNC's . Thanks Bhaiya You inspired me a lot
@abhishekanand6847
@abhishekanand6847 2 жыл бұрын
What ,it means --- Vectorpreorder Traversal(Treenode* root )
@user-vaidesh
@user-vaidesh 10 ай бұрын
It means the function is returning a vector that contains data in the form of integers and the function takes a struct or class of node to represent a treee
@thegreekgoat98
@thegreekgoat98 3 жыл бұрын
What an explanation!!!!! I wrote the C++ code by myself after watching the explanation part...
@vishalwaghmare3130
@vishalwaghmare3130 2 жыл бұрын
This is the best channel I have come across on Data Structures/Coding. Thanks a lot!
@vasudhajha9115
@vasudhajha9115 2 жыл бұрын
Hello Striver! I hope you're doing well :) Thank you so much for making these series that newbies like me can come back to again and again. The passion that you bring to teaching is infectious and in a sea of tutorial hell, your tutorials are a guiding light and the best path to follow! Really appreciate the effort that you put to help countless people. Your work is inspiring.
@tech_Personality
@tech_Personality Ай бұрын
00:35 Iterative preorder traversal in binary tree 01:22 Iterative Preorder Traversal in Binary Tree is done using Stack 02:12 Iterative Preorder Traversal in Binary Tree using Stack 03:11 Iterative Preorder Traversal in Binary Tree using Stack 04:32 Importance of right side effects on mental strength 05:27 Iterative preorder traversal in binary tree is efficient and useful
@Manasidas99
@Manasidas99 Жыл бұрын
Sir your teaching style is really unique.
@manasgupta6647
@manasgupta6647 Жыл бұрын
yep
@shashibhushanrajput2461
@shashibhushanrajput2461 3 жыл бұрын
Nice simple explanation bhaiya.. please make a serious on medium and hard DP problems. Because right now DP is really in demand at Interviews.
@lifehustlers164
@lifehustlers164 Жыл бұрын
Completed 10/54(18% done)!!!
@stith_pragya
@stith_pragya Жыл бұрын
Thank You So Much for this wonderful video.....🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻
@AdityaKumar-be7hx
@AdityaKumar-be7hx Жыл бұрын
We can avoid a few extra checks to write a smaller code: class Solution { public: vector preorderTraversal(TreeNode* root) { vector preorder; stack st; st.push(root); while(!st.empty()){ auto curr=st.top(); st.pop(); if(curr!=nullptr){ preorder.push_back(curr->val); st.push(curr->right); st.push(curr->left); } } return preorder; } };
@ScienceSeekho
@ScienceSeekho 2 жыл бұрын
class Solution { public: // Recursive Traversal. TC: O(# of Nodes) ASC: O(height) void solve(TreeNode* root, vector &ans) { if(root == NULL) return; ans.push_back(root->val); solve(root->left, ans); solve(root->right, ans); } vector preorderTraversal(TreeNode* root) { vector ans; solve(root, ans); return ans; } // Iterative vector preorderTraversal(TreeNode* root) { vector ans; if(root == NULL) return ans; stack s; s.push(root); while(!s.empty()) { TreeNode* node = s.top(); s.pop(); ans.push_back(node->val); if(node->right) s.push(node->right); if(node->left) s.push(node->left); } return ans; } };
@Ayusharma114
@Ayusharma114 2 жыл бұрын
we can use queue too how does that too working while using queue?
@adityababu3405
@adityababu3405 6 ай бұрын
class Solution { public: vector preorderTraversal(TreeNode* root) { vector preorder; if(root == NULL) return preorder; stack st; st.push(root); while(!st.empty()) { root = st.top(); st.pop(); preorder.push_back(root->val); if(root->right != NULL) { st.push(root->right); } if(root->left != NULL) { st.push(root->left); } } return preorder; } };
@noorjabeen731
@noorjabeen731 Жыл бұрын
You are not a king.... You are a kingmaker ✨ 👑
@ayushagrawal6473
@ayushagrawal6473 3 жыл бұрын
Very Nice way. I used to write the preorder code using the inorder traversal with slight updation.
@tanishq2766
@tanishq2766 Жыл бұрын
I did the same, but I found this approach more intuitive !
@sharmanihal99
@sharmanihal99 6 ай бұрын
Just in case someone is looking for the python code for the above: class Solution: def preorderTraversal(self, root: Optional[TreeNode]) -> List[int]: #Iterative Solution if not root:return stack=[root] ans=[] while stack: top=stack.pop() ans.append(top.val) if top.right: stack.append(top.right) if top.left: stack.append(top.left) return ans
@cinime
@cinime 2 жыл бұрын
Understood! So amazing explanation as always, thank you very much!!
@apmotivationakashparmar722
@apmotivationakashparmar722 Ай бұрын
Thank you So much Striver . Please upload Complete String in A-Z Dsa Sheet.
@somyapratapsingh9849
@somyapratapsingh9849 2 жыл бұрын
Looks like watch whole series in one go :))
@per.seus._
@per.seus._ Жыл бұрын
understood🌏
@chiragbansal3364
@chiragbansal3364 3 жыл бұрын
Awesome explanation in just 6 mins
@nawabkhan4916
@nawabkhan4916 3 жыл бұрын
great work, and have lots of sponsor ad so that you can provide great videos.
@rahatsshowcase8614
@rahatsshowcase8614 2 жыл бұрын
Recursion uses already stackspace But we are again usig that stack separately!Something new i likeit
@prathameshjadhav2942
@prathameshjadhav2942 10 ай бұрын
Nice teaching bro.....
@lavanyaprakashjampana933
@lavanyaprakashjampana933 2 жыл бұрын
we love your content and we love you....
@samuelfrank1369
@samuelfrank1369 Жыл бұрын
UNDERSTOOD. THANKS A LOT.
@bhavkushwaha
@bhavkushwaha 7 ай бұрын
Thankyou Striver, Understood!
@PranjalGunjanDiaries
@PranjalGunjanDiaries Жыл бұрын
YOU ARE THE BEST..
@vakhariyajay2224
@vakhariyajay2224 Жыл бұрын
Thank you very much. You are a genius.
@Learnprogramming-q7f
@Learnprogramming-q7f 8 ай бұрын
Thank you Bhaiya
@sonakshibajpai6445
@sonakshibajpai6445 5 ай бұрын
understood!! thanks striver
@sonalisingh7932
@sonalisingh7932 2 ай бұрын
understood👍👍
@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
@BharatKumar-rc8vn
@BharatKumar-rc8vn 4 ай бұрын
cool stuff
@coolgaurav5163
@coolgaurav5163 4 ай бұрын
Understoood
@bhaveshkumar6842
@bhaveshkumar6842 2 жыл бұрын
Immensely grateful for your content
@SambhavJaincs23m060
@SambhavJaincs23m060 2 ай бұрын
0:48 START
@Ergoswami
@Ergoswami Ай бұрын
Completed!
@sagarchauhan3187
@sagarchauhan3187 3 жыл бұрын
Hello Sir, Can you please complete the remaining videos of trees and graph of SDE sheet? please please upload!!🥺🥺
@1tav0
@1tav0 2 жыл бұрын
thank you so much for these series
@sidvyas
@sidvyas 2 жыл бұрын
Bro once you explain most of the time I am able to code the logic by myself. Thanks so much man!!
@DeadPoolx1712
@DeadPoolx1712 Ай бұрын
UNDERSTOOD;
@nagavedareddy5891
@nagavedareddy5891 2 жыл бұрын
Huge respect..❤👏
@anshikagupta5858
@anshikagupta5858 11 ай бұрын
Thankyou Striver:)
@theSeniorSDE
@theSeniorSDE 3 жыл бұрын
Striver, Completed this Question
@mahaveerbana2031
@mahaveerbana2031 2 жыл бұрын
dil se thank you striver
@PrashantSingh-jy6zp
@PrashantSingh-jy6zp 3 жыл бұрын
for best experience go to 0:48
@pradipkumarmukhi
@pradipkumarmukhi 15 күн бұрын
Understood!!!
@codeman3828
@codeman3828 8 ай бұрын
Understood
@culeforever5408
@culeforever5408 Жыл бұрын
understood
@UECAshutoshKumar
@UECAshutoshKumar Жыл бұрын
Thank you sir
@bhushankorg5606
@bhushankorg5606 2 жыл бұрын
Nice explaination!
@jankeshchakravarthy9389
@jankeshchakravarthy9389 Жыл бұрын
Great video, I did not understand where are you printing?
@_AkashRuidas
@_AkashRuidas 2 жыл бұрын
great explaination
@vakhariyajay315
@vakhariyajay315 Жыл бұрын
Thank you very much.
@mriit3025
@mriit3025 8 ай бұрын
I just realized how much I lost the time which should be used to understand the deep logic and the code will be automatic... In my second year of Btech, I had a DS (Data Structure) course, but I just understood the surface logicrememberedember the code mostly which was enough for getting 9/10. as iterative code was not important I was too lazy to understand it. so, my advice to myself is : Did you really understand? really? check? like. I completed a module in DS and just remembered the code, am I able to do a dry run of the code and understand the working! remember don't procastinate!
@aditisharma5745
@aditisharma5745 2 жыл бұрын
You are the best!!
@itsmepiyushsaha
@itsmepiyushsaha 3 жыл бұрын
Superb explanation!
@rohankrishnani
@rohankrishnani 2 жыл бұрын
Please make video on circular queue implementation as well
@gangsta_coder_12
@gangsta_coder_12 3 жыл бұрын
Excellent explanation as always 🔥🔥🔥🔥🔥
@codenchill732
@codenchill732 3 жыл бұрын
You made it so simple !! thanks alot
@Mr.Indianwxvhehsy9191hghgx
@Mr.Indianwxvhehsy9191hghgx 11 ай бұрын
nice
@kasyapdharanikota8570
@kasyapdharanikota8570 3 жыл бұрын
best explanation
@sofikulmallick3766
@sofikulmallick3766 3 жыл бұрын
amazing explanation!
@ramulala9365
@ramulala9365 3 жыл бұрын
Great Explanation!
@rexitspersonal8353
@rexitspersonal8353 2 жыл бұрын
converting recursive preorder to iterative public static List preorderTraversalSecond(Node root) { List ans = new ArrayList(); Stack ds = new Stack(); if (root == null) return ans; Node node = root; while (true) { if (node != null) { ans.add(node.val); ds.push(node); node = node.left; } else { if (ds.isEmpty()) break; node = ds.pop(); node = node.right; } } return ans; }
@D44Aishwarya
@D44Aishwarya Жыл бұрын
thanks a lot sir!
@letmeinthefiend8522
@letmeinthefiend8522 2 жыл бұрын
instead of taking right - left in stack.. can we use queue so that we can use left-right approch .. i learn preoder by rootleftright.. stack approach making confusion right before left
@tejas7379
@tejas7379 2 жыл бұрын
No, it's not possible using queue. Regarding stack approach, think how stack works. Since right goes before left, left node will be popped and explored first, then the right node.
@psibarpsi
@psibarpsi 2 жыл бұрын
@@tejas7379 Why is it not possible to use queue here? Can you please elaborate?
@tejas7379
@tejas7379 2 жыл бұрын
@@psibarpsi In stack we push right and left. Then when we pop we get left first and add its children to stack. So when we pop again we will be traversing all left children. So before we reach right part, we would have completely traversed left sub tree. Now lets say we use queue, we push left first and then right. [left, right]. So when we pop we get left first and then we push left's children to queue, like [right, (lefts children)]. When we pop again we will be exploring right subtree then again left sub tree's children. Which is wrong.
@aakriti1
@aakriti1 2 жыл бұрын
@@tejas7379 Thanks for the nice explanation of why we're using a stack here and why we can't make use of a queue for this question.
@tejas7379
@tejas7379 2 жыл бұрын
@@aakriti1 Glad it helped.
@priyanshupandeypandey7994
@priyanshupandeypandey7994 2 жыл бұрын
jiiiiiiiooooooo guru jiiiiiiii
@akshaibaruah1720
@akshaibaruah1720 2 жыл бұрын
Beego of N (hehe love ur videos)
@mriduljain1981
@mriduljain1981 Жыл бұрын
completed lecture 9 of tree playlist
@abhijeetbasfore6816
@abhijeetbasfore6816 2 жыл бұрын
Thank you
@ganavin3423
@ganavin3423 2 жыл бұрын
understood
@yashmandaviya1356
@yashmandaviya1356 2 жыл бұрын
What is the purpose behind using extra space of the stack , its not improving the time complexity of the preorder traversal?
@as_a_tester
@as_a_tester 2 жыл бұрын
tysm striver bhai
@Ankit.yt_885
@Ankit.yt_885 2 жыл бұрын
Great Work as always! :)
@prateekverma9166
@prateekverma9166 3 жыл бұрын
THANKS STRIVER
@utkarshsharma6650
@utkarshsharma6650 2 жыл бұрын
understoooood. thanks :)
@harshitjaiswal9439
@harshitjaiswal9439 Жыл бұрын
Understoooooooood!!!
@artitech6668
@artitech6668 2 жыл бұрын
Amazing
@singhs_aniket
@singhs_aniket 3 жыл бұрын
Understood 👌👌
@bhavya8608
@bhavya8608 Жыл бұрын
wonderfull!!
@t-anime517
@t-anime517 2 жыл бұрын
Understood😊
@senseiKakashi07
@senseiKakashi07 2 жыл бұрын
this is somewhat like recursion, as recursion uses stack behind the scenes, right call tu wait kar mein pehle left hoke aata hu😂
@kirtikhohal3313
@kirtikhohal3313 2 жыл бұрын
loll
@mahaksharma7827
@mahaksharma7827 10 ай бұрын
👏👏👏👏
@guruprasadkancharla5555
@guruprasadkancharla5555 2 жыл бұрын
why are we using stack? Is there any specific reason? We can do this using Queue also ?
@harshsinha3221
@harshsinha3221 2 жыл бұрын
Queue should be used for Level order traversal, preorder traversal uses recursive call, so you can figure out that recursion itself uses stack DS
@DevanshuAugusty
@DevanshuAugusty Жыл бұрын
shouldn't we take a temp node for root cause we dont want to modifie the data
@sahilkumarsingh8517
@sahilkumarsingh8517 3 жыл бұрын
Understood😁
@gauravkumar0777
@gauravkumar0777 3 жыл бұрын
Op bhaiya
@ajayypalsingh
@ajayypalsingh 2 жыл бұрын
💚
@nileshsinha7869
@nileshsinha7869 3 жыл бұрын
Understood
L10. iterative Inorder Traversal in Binary Tree | C++ | Java | Stack
11:14
Accompanying my daughter to practice dance is so annoying #funny #cute#comedy
00:17
Funny daughter's daily life
Рет қаралды 20 МЛН
The Ultimate Sausage Prank! Watch Their Reactions 😂🌭 #Unexpected
00:17
La La Life Shorts
Рет қаралды 8 МЛН
How I Mastered Data Structures and Algorithms
10:45
Ashish Pratap Singh
Рет қаралды 260 М.
3 Types of Algorithms Every Programmer Needs to Know
13:12
ForrestKnight
Рет қаралды 501 М.
L8. Level Order Traversal of Binary Tree | BFS | C++ | Java
8:57
take U forward
Рет қаралды 415 М.
8 patterns to solve 80% Leetcode problems
7:30
Sahil & Sarra
Рет қаралды 442 М.
LeetCode was HARD until I Learned these 15 Patterns
13:00
Ashish Pratap Singh
Рет қаралды 586 М.