Please likeeee, shareeee and subscribeeeeeeee :) Also follow me at Insta: Striver_79 Please watch it at 0.5x or 0.75x, I mistakenly uploded at 2x 🥺
@rajasethi23413 жыл бұрын
Actually i was watching at 1.75X and suddenly everything changes
@democratcobra3 жыл бұрын
Great Explaination...Thanks for uploading....
@someone-zi8xc3 жыл бұрын
@@rajasethi2341🤣
@preetamkumar8382 жыл бұрын
I watched it whole at 1.5x and realized, later i read the comments 😂😂
@ARUN-ik2nb2 жыл бұрын
@@rajasethi2341 me too🤣
@codermal2 жыл бұрын
We can also see it as preorder-> root left right swap left, right -> root right left reverse -> left right root So in first stack we apply pre-order traversal code, and 2 stack is to reverse it, basically we need 2 stack as we can't move from child to parent in tree
@ashtonronald4 ай бұрын
Thanks! Really helpful!!
@harshitmalhotra86273 жыл бұрын
This year's only video I have watched at 0.5x.
@CosmicWhale77772 жыл бұрын
yes haha
@omkarshendge54382 жыл бұрын
same lol
@shortsmaker03082 ай бұрын
Same 😁
@theSeniorSDE3 жыл бұрын
Striver, had to slow down the video speed to understand it better. 😂 Completed this as well. Steps : 1. initial config : Take the root and put it in the 1st stack. 2. Now, take the top from the 1st stack and put it into the 2nd stack 3. After that, if the top in 2nd stack has left → add it in 1st stack. And if the top in 2nd stack has right → add it in the 1st stack. 4. Now again, take the top from the 1st stack and put it into the 2nd stack. Repeat step 2 & 3 untill 1st stack is empty. 5. Pop the element from the 2nd stack and print.
@takeUforward3 жыл бұрын
Yeah by mistakenly, i uploded it at 2x 😂
@aditiranjan3032 жыл бұрын
Thanks for writing the steps clearly
@rigaut-valorant6105 Жыл бұрын
@@takeUforward and I still watched it at 1.5x
@vishalplayzz2580 Жыл бұрын
@@rigaut-valorant6105 Lmao
@ChefnCoder Жыл бұрын
u explained it really good, any chances u have made notes of tree series cause i am sure it will be really good
@JayPanchal032 ай бұрын
Understood the concept first and then wrote the code on my own and later found out that it is exactly same as in the video got confidence....
@SaurabhSingh-nv9jj2 жыл бұрын
we can also use the ans vector which we are going to return as the second stack, but in the end we will have to reverse the vector before returning it.
@namanchhabra41367 ай бұрын
In Stack 1 we make ROOT RIGHT LEFT ( like we did in preorder -> root left right ) Stack2 is used for reverse : - if we reverse the Stack1 it will give Postorder which is LEFT RIGHT ROOT
@tanishq2766 Жыл бұрын
I was not at all able to come up with this, I hardly give up on problems, this was impossible for me to think fr!
@mohit77175 ай бұрын
You can make everyone understand in 2x too .. that's the quality in you.. Great man!
@26_jamkhandedattatray592 жыл бұрын
i shocked when i played this video at 1.75x speed, after read out description I understand what actually happened😂. Anyway but lec is awsome
@Shourya_performs2 жыл бұрын
xctly
@codding32world502 жыл бұрын
Same here lmao
@Lakshya-f4l5 ай бұрын
😂😂
@valiz2628 Жыл бұрын
explanation is so beautiful , so elegant , just looking like a wow
@devanshmesson27772 жыл бұрын
Those who are unable to understand the intuition behind this, Watch the next video which uses 1 stack, That video is relatively intuitive.
@stith_pragya6 ай бұрын
UNDERSTOOD...Thank You So Much for this wonderful video..........🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻
what is the intution behind this solution...you are directly junmping to the solution
@letsrock73542 жыл бұрын
Rather than taking a stack we can save that in the resultant vector and then reverse it in the end We can save some space
@iamsalonitayal3 жыл бұрын
I was already watching ur vids at 1.5x . But here:-) for a moment I thought my mind was processing the content slowly. lol. Anyways awesome lectures.
@lifehustlers164 Жыл бұрын
Completed 12/54(22% done) !!!
@nandkishor9992 жыл бұрын
hello sir this series is really great. I tried many series on the youtube but your's one is best thank you so much.
@K_EN_VisheshSaini Жыл бұрын
We can just use the preorder traversal code and reverse the insertion of left and right nodes in the stack and also the array in the end before returning it. Code:- class Solution { public: vector postorderTraversal(TreeNode* root) { vector ans; if(root == NULL) return ans; stack st; st.push(root); while(!st.empty()){ TreeNode *temp = st.top(); st.pop(); ans.push_back(temp->val); if(temp->left!=NULL) st.push(temp->left); if(temp->right!=NULL) st.push(temp->right); } reverse(ans.begin(), ans.end()); return ans; } };
@bambu-mq9zg2 жыл бұрын
we can also use queue and stack for it... like root >right>left then store it into queue from left class Solution: # Return a list containing the inorder traversal of the given tree def postOrder(self,node): s=[node] l=1 ans=deque() while l!=0: a=s.pop() l-=1 ans.appendleft(a.data) if a.left!=None: s+=[a.left] l+=1 if a.right!=None: s+=[a.right] l+=1 ans=list(ans) return ans
@Rebantaprat2 жыл бұрын
Striver what is the intuition for using 2 stack 🤔?
@tonyconor6812 жыл бұрын
I puted as usual 2x then suddenly I feel it's like 3x , then I did 1.25 final calmly understood his words holy moly
@pranjal11003 жыл бұрын
Was watching the playlist at 1.35x when I came across this XD
I used to watch your videos 1.5x. But when I did it in this video Ahhhh!! NFS rocks........
@nihalshukla77183 жыл бұрын
I was refreshing again refreshing then open in new tab.. after that I came to comment section.. then I realize my pc is working fine.
@bhavkushwaha7 ай бұрын
Thankyou Striver, Understood!
@andersonbhat68853 жыл бұрын
Striver bhaiya as a first timer looking at this algo i'm not sure whether we should be able to think of such an algo on our own ,cause i couldn't think of it or are we expected to get this after some practice ?
@takeUforward3 жыл бұрын
There are certain things you have to know in order to do.
@justarandomguy61062 жыл бұрын
@@takeUforward means we have to remedy these types of solution like algorithm
@samuelfrank1369 Жыл бұрын
UNDERSTOOD. THANKS A LOT.
@raj-nq8ke2 жыл бұрын
begins at 0:44
@arnabdutta46622 жыл бұрын
if stack 2 is made integer dayatype then space requirement would be reduced :)
@raviketan29573 жыл бұрын
Thanks for whatever u r doing.
@5076_GidijalaDineshSurya2 ай бұрын
Loved the lecture striver! I have a doubt, Why not just use a vector instead of 2nd stack and then reverse it? Why did we use 2 stacks?
@piyushmahale90244 ай бұрын
This is the first video that i have seen at normal speed 😂
@ritamkabiraj8035 Жыл бұрын
st2.push(node) instead st2.add(node) [Time 3:09]
@liteni3682 жыл бұрын
Striver bhaia tussi great ho
@AnkitOli-d2g Жыл бұрын
Can we directly add the elements to output arrayList and print it in reverse order,without using st2. Will it make space complexity a bit efficient?
@rupalikumari88295 ай бұрын
Understood : )
@mriduljain1981 Жыл бұрын
completed lecture 11 of Tree playlist.
@alienx23673 жыл бұрын
Why did you suddenly tuned into Eminem
@ishankbansal92393 жыл бұрын
😂🤣
@shubamgoswami3 жыл бұрын
EMINEM IS RAP GOD AND STRIVER IS INTIUATION GOD
@nawabkhan49162 жыл бұрын
great work, and have lots of sponsor ad so that you can provide great videos.
@mannmehta4841Ай бұрын
Easier approach with only one stack without infinite loop, but yes O(N) extra space - 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; } if (node->right != NULL and !vis[node->right]) { st.push(node->right); continue; } cout
@parthh11127 ай бұрын
void inorder(TreeNode *&root) { stack s1, s2; s1.push(root); while (s1.size() > 0) { TreeNode *x = s1.top(); s2.push(x); s1.pop(); if (x->left) s1.push(x->left); if (x->right) s1.push(x->right); } while (s2.size() > 0) { cout val
@debangshubanerjee13112 жыл бұрын
What is the intuition behind using 2 stacks?
@subhajitdey135 Жыл бұрын
In Preorder and inorder we were going from parent to child but as you can see in postorder we need to keep the track of parent before moving to the right child. So to keep the track we are using two stacks.
@UECAshutoshKumar Жыл бұрын
Thank you sir
@t-anime5172 жыл бұрын
Understood😊
@rishidubey5037 Жыл бұрын
Great Content.
@jasonbrody46183 жыл бұрын
Liked. Cfbr will watch after some time
@apmotivationakashparmar722Ай бұрын
Thank you so much.
@ARUN-ik2nb2 жыл бұрын
Short and sweet 👌
@rishabhshairy972Ай бұрын
We can use One Stack and One List List can be used to store values and return public List postorderTraversal(TreeNode root) { List postOrderList = new ArrayList(); if (root == null) { return postOrderList; } Stack opStack = new Stack(); opStack.push(root); while (!opStack.isEmpty()) { root = opStack.pop(); if (root.left != null) { opStack.push(root.left); } if (root.right != null) { opStack.push(root.right); } postOrderList.add(root.data); } Collections.reverse(postOrderList); return postOrderList; }
@smitdumore10642 жыл бұрын
Making the video duration short by speeding it up is a genius strategy to get more clicks,
@snehilsinha56242 жыл бұрын
Why don't we store the postorder traversal in the final output vector itself and reverse it at the last rather than using an extra stack for reversal ?
@vakhariyajay2224 Жыл бұрын
Thank you very much. You are a genius.
@akashverma57564 ай бұрын
what is intuition behind this solution? I think this solution is inspired from from the fact that post order traversal is opposite of reverse preorder traversal. (left - right - node) == !(node - right - left)
@DeadPoolx1712Ай бұрын
UNDERSTOOD;
@lourduradjou182 Жыл бұрын
super hero striver
@anshikagupta585811 ай бұрын
Thankyou Striver:)
@rishabmalhotra69802 жыл бұрын
at 0.5 speed it seems like striver is begging in last LOL
mujhe laga mera dimag kaam nhi kar paa raha ..fir dekha ..video thoda jyada hi speed hai 😂😂😂😂😂😂😂😂
@fantasyillusion2 жыл бұрын
for the 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 postorderTraversal(self, root: Optional[TreeNode]) -> List[int]: postorder = [] if root==None: return postorder st1,st2=[root],[] while st1: root = st1.pop() st2.append(root) if root.left: st1.append(root.left) if root.right: st1.append(root.right) while st2: postorder.append(st2.pop().val) return postorder
@subhamsingh95532 жыл бұрын
Bro if you return st2[::-1] instead of postorder array(you don't need a third list) you will get the answer second while loop is not necessary
@atharvacodes37053 жыл бұрын
The space complexity should be O(N) right? Even though we're using two stacks, in the end, sum of number of nodes in both the stacks is N, where N in the number of nodes in the tree.
@lakshay69942 жыл бұрын
yes
@codding32world502 жыл бұрын
yess
@lavanyaprakashjampana9332 жыл бұрын
we love your content and we love you....🖤
@coolgaurav51634 ай бұрын
Understood
@ashitasrivastava681 Жыл бұрын
UNDERSTOOD!
@_hulk748 Жыл бұрын
Understood sir🙏🙇♂❤
@dhivyashri65543 жыл бұрын
hello bhaiya! thank you so much for the video, but i think it's sped up could u check that if possible
me watching the whole series at 2x , suddenly this video at 4x :D
@PrakharKulshrestha-q6e Жыл бұрын
understood
@SURISETTISRIRAMNITAP Жыл бұрын
can we use vector in place of second stack as it is just holding the node data, if use the vector we can reverse it at the end!!
@ak67373 Жыл бұрын
If we add in list instead of 2nd stack and then reverse it?
@Student-j4u2 ай бұрын
yeah we can
@deepbytes48542 жыл бұрын
# Python code stack1 = [root] # initialise stack with root stack2 = [] while stack1 : node = stack1.pop() # pop from stack1 if node is not None : stack2.append(node.val) # in stack2 append node.val if node.left is not None : stack1.append(node.left) if node.right is not None : stack1.append(node.right) return stack2[::-1] # return stack2 in reverse
@ornatetrout Жыл бұрын
How SC is 2N? At any point of time we are going to store a node only once. The node can be present in stack 1 or in stack 2. So, at max sum of size of stack 1 and stack 2 at any point of time can be N. So, SC should be O(N). Striver bro please clarify my doubt.
@reshihashim40942 жыл бұрын
1kth like is mine ❤️🔥
@priyaanshusoni2 ай бұрын
We can Solve this problem by O(N) also Bhaiya , /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} * }; */ class Solution { public: // void PostOrder(TreeNode* root , vector&ans){ // if(root==nullptr) return ; // PostOrder(root->left,ans); // PostOrder(root->right,ans); // ans.push_back(root->val); // } vector postorderTraversal(TreeNode* root) { // vectorans; // PostOrder(root,ans); // return ans; //post order traversal using one stack stack st; vectorans; if(root==NULL){ return ans; } st.push(root); while(!st.empty()){ TreeNode* temp =st.top(); st.pop(); ans.push_back(temp->val); if(temp->left) st.push(temp->left); if(temp->right) st.push(temp->right); } reverse(ans.begin() , ans.end()); return ans; } }; AND PLEASE PLEASE BRING LATEST TREE & GRAPH SERIES .
@1tav02 жыл бұрын
Thank you so much striver for these series. You are a great help to people like me
@altamashsabri81422 жыл бұрын
Understood
@shivangisrivastava11583 жыл бұрын
Amazing video!
@rahulsrivastava10402 жыл бұрын
U r in final year ?
@abhaymandal490310 ай бұрын
I think tc will be 0(2n)
@tusharrustagi57292 жыл бұрын
understood
@lone_wolf77213 жыл бұрын
instead of using another stack can't we use vector and reverse it?
@takeUforward3 жыл бұрын
Yeah as you wish. Concept stays the same.
@ayushop19213 жыл бұрын
@@takeUforward so bhaiya in this approach the space complexity will be O(1) na ?\
@shardulwagh9042 жыл бұрын
@@ayushop1921 In the worst case we add all nodes of the tree to the stack, making it O(n)
@deepakgurjar37462 жыл бұрын
but for this u heave to take care of node and value...in vector u push data but for check whether left of node or right of node exist there must be a node u are checking not data\value :)
@mixshots180110 ай бұрын
My solution for iterative post-order traversal and it passed all test cases in leetcode: /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} * }; */ class Solution { public: vector postorderTraversal(TreeNode* root) { vector v; auto node=root; stack st; while(true){ if(node!=NULL){ st.push(node); v.push_back(node->val); node=node->right; } else{ if(st.empty())break; auto temp=st.top(); st.pop(); node=temp->left; } } reverse(v.begin(),v.end()); return v; } }; TC=O(N+N)