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

  Рет қаралды 334,588

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.❤💯
@abhishekanand6847
@abhishekanand6847 2 жыл бұрын
What ,it means --- Vectorpreorder Traversal(Treenode* root )
@user-vaidesh
@user-vaidesh 9 ай бұрын
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
@Ishan301
@Ishan301 3 жыл бұрын
Striver is life saviour for tier-3 college guys like me. Thank you.
@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.😊
@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.
@thegreekgoat98
@thegreekgoat98 3 жыл бұрын
What an explanation!!!!! I wrote the C++ code by myself after watching the explanation part...
@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
@vishalwaghmare3130
@vishalwaghmare3130 2 жыл бұрын
This is the best channel I have come across on Data Structures/Coding. Thanks a lot!
@Manasidas99
@Manasidas99 Жыл бұрын
Sir your teaching style is really unique.
@manasgupta6647
@manasgupta6647 11 ай бұрын
yep
@lifehustlers164
@lifehustlers164 Жыл бұрын
Completed 10/54(18% done)!!!
@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
@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; } };
@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.
@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.
@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
@noorjabeen731
@noorjabeen731 Жыл бұрын
You are not a king.... You are a kingmaker ✨ 👑
@apmotivationakashparmar722
@apmotivationakashparmar722 Ай бұрын
Thank you So much Striver . Please upload Complete String in A-Z Dsa Sheet.
@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; } };
@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 !
@per.seus._
@per.seus._ Жыл бұрын
understood🌏
@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?
@cinime
@cinime 2 жыл бұрын
Understood! So amazing explanation as always, thank you very much!!
@nawabkhan4916
@nawabkhan4916 2 жыл бұрын
great work, and have lots of sponsor ad so that you can provide great videos.
@somyapratapsingh9849
@somyapratapsingh9849 2 жыл бұрын
Looks like watch whole series in one go :))
@rahatsshowcase8614
@rahatsshowcase8614 2 жыл бұрын
Recursion uses already stackspace But we are again usig that stack separately!Something new i likeit
@chiragbansal3364
@chiragbansal3364 3 жыл бұрын
Awesome explanation in just 6 mins
@bhavkushwaha
@bhavkushwaha 7 ай бұрын
Thankyou Striver, Understood!
@samuelfrank1369
@samuelfrank1369 Жыл бұрын
UNDERSTOOD. THANKS A LOT.
@prathameshjadhav2942
@prathameshjadhav2942 10 ай бұрын
Nice teaching bro.....
@lavanyaprakashjampana933
@lavanyaprakashjampana933 2 жыл бұрын
we love your content and we love you....
@PranjalGunjanDiaries
@PranjalGunjanDiaries Жыл бұрын
YOU ARE THE BEST..
@jankeshchakravarthy9389
@jankeshchakravarthy9389 Жыл бұрын
Great video, I did not understand where are you printing?
@coolgaurav5163
@coolgaurav5163 4 ай бұрын
Understoood
@Ergoswami
@Ergoswami Ай бұрын
Completed!
@sonalisingh7932
@sonalisingh7932 2 ай бұрын
understood👍👍
@BharatKumar-rc8vn
@BharatKumar-rc8vn 4 ай бұрын
cool stuff
@sagarchauhan3187
@sagarchauhan3187 3 жыл бұрын
Hello Sir, Can you please complete the remaining videos of trees and graph of SDE sheet? please please upload!!🥺🥺
@vakhariyajay2224
@vakhariyajay2224 Жыл бұрын
Thank you very much. You are a genius.
@yashmandaviya1356
@yashmandaviya1356 2 жыл бұрын
What is the purpose behind using extra space of the stack , its not improving the time complexity of the preorder traversal?
@sidvyas
@sidvyas 2 жыл бұрын
Bro once you explain most of the time I am able to code the logic by myself. Thanks so much man!!
@sonakshibajpai6445
@sonakshibajpai6445 5 ай бұрын
understood!! thanks striver
@pradipkumarmukhi
@pradipkumarmukhi 11 күн бұрын
Understood!!!
@DeadPoolx1712
@DeadPoolx1712 Ай бұрын
UNDERSTOOD;
@theSeniorSDE
@theSeniorSDE 3 жыл бұрын
Striver, Completed this Question
@bhaveshkumar6842
@bhaveshkumar6842 2 жыл бұрын
Immensely grateful for your content
@anshikagupta5858
@anshikagupta5858 11 ай бұрын
Thankyou Striver:)
@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!
@nagavedareddy5891
@nagavedareddy5891 2 жыл бұрын
Huge respect..❤👏
@rohankrishnani
@rohankrishnani 2 жыл бұрын
Please make video on circular queue implementation as well
@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.
@SambhavJaincs23m060
@SambhavJaincs23m060 2 ай бұрын
0:48 START
@Learnprogramming-q7f
@Learnprogramming-q7f 8 ай бұрын
Thank you Bhaiya
@DevanshuAugusty
@DevanshuAugusty Жыл бұрын
shouldn't we take a temp node for root cause we dont want to modifie the data
@1tav0
@1tav0 2 жыл бұрын
thank you so much for these series
@codeman3828
@codeman3828 8 ай бұрын
Understood
@PrashantSingh-jy6zp
@PrashantSingh-jy6zp 3 жыл бұрын
for best experience go to 0:48
@mahaveerbana2031
@mahaveerbana2031 2 жыл бұрын
dil se thank you striver
@itsmepiyushsaha
@itsmepiyushsaha 3 жыл бұрын
Superb explanation!
@UECAshutoshKumar
@UECAshutoshKumar Жыл бұрын
Thank you sir
@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
@codenchill732
@codenchill732 3 жыл бұрын
You made it so simple !! thanks alot
@gangsta_coder_12
@gangsta_coder_12 3 жыл бұрын
Excellent explanation as always 🔥🔥🔥🔥🔥
@aditisharma5745
@aditisharma5745 2 жыл бұрын
You are the best!!
@culeforever5408
@culeforever5408 Жыл бұрын
understood
@vakhariyajay315
@vakhariyajay315 Жыл бұрын
Thank you very much.
@aryansingh5198
@aryansingh5198 Ай бұрын
Why did we traverse the right sub tree first in pre order Arent we supposed to go from Root Left Right ???
@bhushankorg5606
@bhushankorg5606 2 жыл бұрын
Nice explaination!
@_AkashRuidas
@_AkashRuidas 2 жыл бұрын
great explaination
@mahaksharma7827
@mahaksharma7827 9 ай бұрын
👏👏👏👏
@kasyapdharanikota8570
@kasyapdharanikota8570 3 жыл бұрын
best explanation
@Mr.Indianwxvhehsy9191hghgx
@Mr.Indianwxvhehsy9191hghgx 11 ай бұрын
nice
@sofikulmallick3766
@sofikulmallick3766 3 жыл бұрын
amazing explanation!
@ramulala9365
@ramulala9365 3 жыл бұрын
Great Explanation!
@mriduljain1981
@mriduljain1981 Жыл бұрын
completed lecture 9 of tree playlist
@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 Жыл бұрын
Understoooooooood!!!
@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; }
@priyanshupandeypandey7994
@priyanshupandeypandey7994 2 жыл бұрын
jiiiiiiiooooooo guru jiiiiiiii
@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
@laxmikantkatre4579
@laxmikantkatre4579 Жыл бұрын
I used little different approach to solve this question. I am pushing left first and then right in stack. Below is the Accepted Code: //Iterative Solution public List preorderTraversal(TreeNode root) { List ans = new LinkedList(); if(root == null) return ans; Stack stack = new Stack(); TreeNode node = root; while(true) { if(node != null) { stack.push(node); ans.add(node.val); node = node.left; } else { if(stack.isEmpty()) break; node = stack.pop(); node = node.right; } } return ans; }
@singhs_aniket
@singhs_aniket 3 жыл бұрын
Understood 👌👌
@koushik1261
@koushik1261 3 жыл бұрын
Bhaiya, in interviews do they ask explicitly to implement in iterative/recursive method?
@D44Aishwarya
@D44Aishwarya Жыл бұрын
thanks a lot sir!
@prateekverma9166
@prateekverma9166 3 жыл бұрын
THANKS STRIVER
@t-anime517
@t-anime517 2 жыл бұрын
Understood😊
@as_a_tester
@as_a_tester 2 жыл бұрын
tysm striver bhai
@abhijeetbasfore6816
@abhijeetbasfore6816 2 жыл бұрын
Thank you
@ravisaharan219
@ravisaharan219 3 ай бұрын
Can someone please give an example of worst case scenario of space complexity where stack size goes up to O(N)
@RAJ-cc4xn
@RAJ-cc4xn Жыл бұрын
Can we do this Using Queue ? Anyone Please answer
@ganavin3423
@ganavin3423 2 жыл бұрын
understood
@SachinSingh-id8mf
@SachinSingh-id8mf 2 жыл бұрын
bhaiya if you write a code in the video.. this would be more easy to understand.
@akshaibaruah1720
@akshaibaruah1720 2 жыл бұрын
Beego of N (hehe love ur videos)
@utkarshsharma6650
@utkarshsharma6650 2 жыл бұрын
understoooood. thanks :)
@bhavya8608
@bhavya8608 Жыл бұрын
wonderfull!!
L10. iterative Inorder Traversal in Binary Tree | C++ | Java | Stack
11:14
3 Types of Algorithms Every Programmer Needs to Know
13:12
ForrestKnight
Рет қаралды 499 М.
Premature Optimization
12:39
CodeAesthetic
Рет қаралды 834 М.
L14. Maximum Depth in Binary Tree | Height of Binary Tree | C++ | Java
8:05
Best Books for Learning Data Structures and Algorithms
14:01
Engineering with Utsav
Рет қаралды 374 М.
8 patterns to solve 80% Leetcode problems
7:30
Sahil & Sarra
Рет қаралды 438 М.
L8. Level Order Traversal of Binary Tree | BFS | C++ | Java
8:57
take U forward
Рет қаралды 414 М.