L37. Morris Traversal | Preorder | Inorder | C++ | Java

  Рет қаралды 265,779

take U forward

take U forward

Күн бұрын

Пікірлер: 293
@takeUforward
@takeUforward 3 жыл бұрын
Please likeeee, shareeee and subscribeeeeeeee :) Also follow me at Insta: Striver_79 Aaaye hi ho, toh series bhi dekh lo ;)
@2020-t1z
@2020-t1z 3 жыл бұрын
Bilkul.
@deepaksarvepalli2344
@deepaksarvepalli2344 3 жыл бұрын
Just informed to my friends that video is out 😁
@PrashantSingh-ej8lz
@PrashantSingh-ej8lz 3 жыл бұрын
Hey striver please add morris postorder traversal
@priyanshkumar17
@priyanshkumar17 8 ай бұрын
Wonderful explanation bhaiya...
@uRamPlus
@uRamPlus 3 жыл бұрын
Self notes: In-order Morris Traversal: 🌳 1st case: if left is null, print current node and go right 🌳 2nd case: before going left, make right most node on left subtree connected to current node, then go left 🌳 3rd case: if thread is already pointed to current node, then remove the thread
@TheElevatedGuy
@TheElevatedGuy 2 жыл бұрын
Great Job Thanks!
@CostaKazistov
@CostaKazistov 2 жыл бұрын
Came here for this. Thank you! ✍👍
@gandhijainamgunvantkumar6783
@gandhijainamgunvantkumar6783 2 жыл бұрын
In each video I find your comment to take the notes :)
@AbhishekYadav-rm4hw
@AbhishekYadav-rm4hw 2 жыл бұрын
In 3rd case shouldn't it be Remove the thread ..and THEN PRINT NODE AND GO RIGHT??
@parthsalat
@parthsalat 2 жыл бұрын
@@AbhishekYadav-rm4hw Yes, right. You can add it to 'your' notes
@deepanshukhanna1780
@deepanshukhanna1780 3 жыл бұрын
Looking at the views of this video , I think this is the reason people don't make quality stuff, today's generation is more interested in watching those hypothetical motivational videos but not interested in watching what it takes to achieve your goals. Hat's off to you. Keep making such videos. Thanks a lot.
@KunalSinghStatus
@KunalSinghStatus 3 жыл бұрын
Right sir ❤️
@vivekjaiswar6825
@vivekjaiswar6825 3 жыл бұрын
They are more interested in "How to get into google" type of videos.
@01_abhijeet49
@01_abhijeet49 2 жыл бұрын
Cute
@AdityaKumar-be7hx
@AdityaKumar-be7hx Жыл бұрын
@@vivekjaiswar6825 True. I will also add that these are the contents of videos that actually answers the question "How to get into G?" :) By becoming truly great in solving problems
@thomasshelby6780
@thomasshelby6780 4 ай бұрын
bro it doesn't take a lot to mind one's own business. Stop taking a jibe at others. Those guys might be good at thousand things that you aren't. Better if you quit perpetuating a rat race in tech that is there already.
@sparshsharma6068
@sparshsharma6068 3 жыл бұрын
By far THE BEST explanation of Morris traversal on youtube!!! Maza aagya bhaiya🔥🔥 and Happy birthday bhaiya. Thank you for being the teacher and a senior we all wanted in our lives🎉🎉 Here's the postorder morris traversal(similar to preorder), the only changes are instead of finding the rightmost node in the left subtree, we find the leftmost node in the right subtree. Also, we need to keep adding the node values in the head so as to maintain the LRN order of postorder. public List postorderTraversal(TreeNode root) { List arr = new LinkedList(); TreeNode curr = root; while(curr != null){ if(curr.right == null){ arr.add(0,curr.val); curr = curr.left; } else{ TreeNode temp = curr.right; while(temp.left != null && temp.left != curr) temp = temp.left; if(temp.left == null){ temp.left = curr; arr.add(0,curr.val); curr = curr.right; } else{ temp.left = null; curr = curr.left; } } } return arr; }
@kannank4269
@kannank4269 Жыл бұрын
can you please explain the intuition of connecting leftmost node of right subtree to cur
@iamnoob7593
@iamnoob7593 4 ай бұрын
Just replace left with right and right with left of preorder code. Thanks
@abhisheknag1929
@abhisheknag1929 2 ай бұрын
@@iamnoob7593 it is wrong
@anmolkohli6299
@anmolkohli6299 3 жыл бұрын
Crystal clear explanation! Understanding it from reading articles was really difficult and you made it so simple. Thank you !🙌
@CostaKazistov
@CostaKazistov 2 жыл бұрын
I agree. Reading about this algorithm is one thing. Watching the explanation on KZbin makes it soooo much clearer. 🔊
@priyanshkumar17
@priyanshkumar17 8 ай бұрын
Yeah, you're right.@@CostaKazistov
@slowedReverbJunction
@slowedReverbJunction 3 жыл бұрын
Seriously bhaiya You are man of your words You said that this will be the best explanation of Morris Traversal And you proved it Hats off to to bhaiya 💪👏👏 Thank u sooo much for this awesome content ♥️♥️
@karanveersingh5535
@karanveersingh5535 3 жыл бұрын
Bro kidda? 2nd year ki 3rd.
@akashgupta13
@akashgupta13 2 жыл бұрын
@@karanveersingh5535 😂
@parthsalat
@parthsalat 2 жыл бұрын
@@akashgupta13 😂
@shreyanshpatil8303
@shreyanshpatil8303 3 ай бұрын
DRY RUN OF THE INPUT Start at the Root: curr = 1 Current ans = [] First Iteration: curr = 1 curr->left = 2, so we find the inorder predecessor (prev) in 2's subtree. Traverse prev to the rightmost node of 2's subtree: prev = 2 → prev->right = 5 prev = 5 → prev->right = 6 prev = 6 Set 6->right = 1 (temporary link). Move curr = 2. Current ans = [] Second Iteration: curr = 2 curr->left = 4, find the inorder predecessor (prev) in 4's subtree: prev = 4 (rightmost node) Set 4->right = 2. Move curr = 4. Current ans = [] Third Iteration: curr = 4 curr->left = NULL, so append 4 to ans. Move curr = 2 (via 4->right). Current ans = [4] Fourth Iteration: curr = 2 (from temporary link) 4->right is already set to 2, indicating the left subtree is processed. Remove temporary link 4->right = NULL. Append 2 to ans. Move curr = 5 (right child of 2). Current ans = [4, 2] Fifth Iteration: curr = 5 curr->left = NULL, so append 5 to ans. Move curr = 6 (right child of 5). Current ans = [4, 2, 5] Sixth Iteration: curr = 6 curr->left = NULL, so append 6 to ans. Move curr = 1 (via 6->right). Current ans = [4, 2, 5, 6] Seventh Iteration: curr = 1 (from temporary link) 6->right is already set to 1, indicating the left subtree is processed. Remove temporary link 6->right = NULL. Append 1 to ans. Move curr = 3 (right child of 1). Current ans = [4, 2, 5, 6, 1] Eighth Iteration: curr = 3 curr->left = NULL, so append 3 to ans. Move curr = NULL. Current ans = [4, 2, 5, 6, 1, 3] End: curr = NULL, and there are no more nodes to process.
@shwetanksingh5208
@shwetanksingh5208 2 жыл бұрын
For post order Morris Traversal-> We will modify the preorder morris traversal from root->left->right to root->right->left. For this we need to do one more change. While in normal preorder and inorder, we were creating thread from right most node of left subtree to present node, here we will create thread from left most node of right subtree to present node as here we have to cover right subtree before left subtree class Solution { public: vector postorderTraversal(TreeNode* root) { vector ans; while(root) { TreeNode *curr = root;//We will create thread from left most node of right subtree to present node and will //travell to that node using curr if(curr->right)//if root has right child //We can't push directly this root node val to ans as we are not sure whether we are here //thorough thread link after covering right subtree or we are here for the first time { curr = curr->right; while(curr->left && curr->left != root)//go to left most node of right subtree curr=curr->left; if(curr->left != root)//not threaded yet { ans.push_back(root->val);//it means root was visited for first time and this is modified preorder hence //push this node's val to ans curr->left = root;//create the thread root = root->right;//go to right to cover right subtree as modified preorder is root->right->left } else//was threaded { curr->left = NULL;//break the thread root = root->left;//right subtree has been covered hence now cover the left one //no need to push this node value as we are here for the second time using thread //link } } else//root hasn't right child { ans.push_back(root->val);//modified preorder is root->right->left hence push this value before going to left root = root->left; } } reverse(ans.begin(),ans.end());//reversing root->right->left to left->right->root to make it post order return ans; } };
@AlbertoRodriguez-oe6jo
@AlbertoRodriguez-oe6jo 2 жыл бұрын
This is not O(1) space complexity.
@shwetanksingh5208
@shwetanksingh5208 2 жыл бұрын
@@AlbertoRodriguez-oe6jo do you consider storage space of ans in space complexity?
@Rajat_maurya
@Rajat_maurya 2 жыл бұрын
thanks
@harshanand4501
@harshanand4501 3 жыл бұрын
I studied it from book but was unable to understand it. But after watching your video understood it completely. Great Explanation!
@abhijeetmishra3804
@abhijeetmishra3804 Жыл бұрын
In starting i was like this video need improvement but after a certain time it was STRIVER show and this was so easy to understand. Thank You Striver ...
@amanbhadani8840
@amanbhadani8840 3 жыл бұрын
This is indeed the best explanation of this series, truly crystal clear.Kudos to your work for the programming community Bhaiya.
@pankajvishwakarma3124
@pankajvishwakarma3124 4 ай бұрын
✅ Postorder || Morris Traversal vector postorderTraversal(TreeNode* root) { vector postorder; TreeNode* cur= root; while(cur){ if(!cur->right){ postorder.push_back(cur->val); cur=cur->left; }else{ TreeNode* prev=cur->right; while(prev->left && prev->left!=cur){ prev=prev->left; } if(prev->left==NULL){ prev->left=cur; postorder.push_back(cur->val); cur=cur->right; }else{ prev->left=NULL; cur=cur->left; } } } reverse(postorder.begin(),postorder.end()); return postorder; }
@SibiRanganathL
@SibiRanganathL 21 күн бұрын
Man that''s a great intution
@JamesHalpert8555
@JamesHalpert8555 2 жыл бұрын
Beautifully explained!! I watched another lecture but was struggling to come up with morris version for other traversals,now it's crystal clear thank you!!
@sauravchandra10
@sauravchandra10 11 ай бұрын
Every time I watch it, things become more clearer. This is the third time in 6 months.
@sahilverma1991
@sahilverma1991 Ай бұрын
Morris Traversal for Post Order Traversal // postorder -> left, right, node // reverse of postorder is -> node right left // we modify Code of Morris Traversal for preorder ( which is node, left , right) vector ans; while(root){ if(root->right == nullptr){ ans.push_back(root->val); root = root->left; }else{ auto temp = root->right; while(temp->left && temp->left != root){ temp = temp->left; } if(temp->left == nullptr){ temp->left = root; ans.push_back(root->val); root = root->right; }else{ temp->left = nullptr; root = root->left; } } } reverse(ans.begin(), ans.end()); return ans;
@shriyanshjain4444
@shriyanshjain4444 Жыл бұрын
To be honest, this was the toughest video of the playlist so far, and obviously not the easiest! 100/100 for the efforts!
@studshelper9901
@studshelper9901 5 ай бұрын
preorder : we must push curr->val in preorder before visiting left inorder: we will visit left then push the curr->val
@paryt7696
@paryt7696 3 жыл бұрын
1 upvote, best explaination brother, and congo for the offers from facebook and google, this news had definitely shut the mouth of haters
@aryanchaurasia1081
@aryanchaurasia1081 Жыл бұрын
Amazingly explained ,the intuition has just fit into my head forever❤️
@deepakjain4481
@deepakjain4481 Жыл бұрын
you are best man no one even touch these type of topics
@saisardesai5548
@saisardesai5548 4 ай бұрын
never seen an explaination like this wonderful!
@kashishsharma6809
@kashishsharma6809 3 жыл бұрын
Application of this algo = change links in tree ques. Eg flatten the tree ( next ques of this playlist). 👍👍
@santanu29
@santanu29 Жыл бұрын
Great Explanation. A bit of a heavy topic, takes time to wrap your head around. Will revisit after a week.
@shreyashachoudhary480
@shreyashachoudhary480 2 жыл бұрын
Really amazing explanation. Thanks. Though that AMORTIZED time complexity confuses a bit.
@jatinsalve364
@jatinsalve364 8 ай бұрын
Interviewer will ask this question like , do the traversal without using recursion or-any extraspace.
@zsa208
@zsa208 Ай бұрын
Very good explanation. Appreciated
@stith_pragya
@stith_pragya Жыл бұрын
Thank You So Much for this wonderful video............🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻
@umber3117
@umber3117 11 ай бұрын
Best explanation.Thanks a lot for making these videos.
@adityan5302
@adityan5302 2 жыл бұрын
Almost completed my Binary Trees trees. If I get placed in a good company, the credit goes to you. Love you bro
@073.iyasmeenmahilang3
@073.iyasmeenmahilang3 4 ай бұрын
You did?
@natanshisharma8828
@natanshisharma8828 4 ай бұрын
@@073.iyasmeenmahilang3 lol
@anshumanprasad5410
@anshumanprasad5410 3 жыл бұрын
thank you so much after struggling so much on this topic, I watched your video.
@mayankdham8917
@mayankdham8917 3 жыл бұрын
Best explanation of morris traversal..
@LazyCoder20
@LazyCoder20 2 ай бұрын
That was the best explanation. Truly epic bhai.
@Ktr_792
@Ktr_792 7 ай бұрын
Nothing Better Than this explanation
@preetkatiyar969
@preetkatiyar969 2 жыл бұрын
Best explanation. No one can explain better than this way.
@amanali9501
@amanali9501 2 жыл бұрын
why there is a need to remove a thread ? if it exists on that element it mean its left side is visited , so one can go to right directly
@bharatht5595
@bharatht5595 2 жыл бұрын
Clear explanation from scratch. Thanks
@iamnoob7593
@iamnoob7593 4 ай бұрын
Superb striver , Man ur excellent
@aviralgoel5709
@aviralgoel5709 Жыл бұрын
Actual video starts @2:10
@anmolswarnkar7707
@anmolswarnkar7707 3 жыл бұрын
This was very well explained, thanks a lot.
@mohitsingh7793
@mohitsingh7793 2 жыл бұрын
Crystal and Clear Explanation for such a difficult topic❤
@devanshtiwari9163
@devanshtiwari9163 Жыл бұрын
Great Explanation till now for Morris Traversal.
@apmotivationakashparmar722
@apmotivationakashparmar722 19 күн бұрын
Thank you So much Striver !
@Flash-qr5oh
@Flash-qr5oh Жыл бұрын
he really made the code easy....
@subhajyotisaha1020
@subhajyotisaha1020 2 ай бұрын
Great video, Thanks you a lot...
@surabhichoubey2987
@surabhichoubey2987 2 жыл бұрын
Literally it was seems Soo horrible now it's seems so easy just because of you thanks a lot ❤️
@nagasrikuncharapu3736
@nagasrikuncharapu3736 5 ай бұрын
It took time to understand this, but it's worth it.
@giteshkhanna2633
@giteshkhanna2633 4 күн бұрын
Great content.
@VinodKumar-by4pt
@VinodKumar-by4pt Жыл бұрын
One of the easiest explanation ever, Thank you strive Bhaiya❤
@akshatlumb409
@akshatlumb409 2 ай бұрын
4k video really makes a difference
@morhadi
@morhadi 6 ай бұрын
The dopamine rush after your code runs on first try >>>
@sumitgupta310
@sumitgupta310 6 ай бұрын
what a amazing Explaination man!!
@MayankLC93
@MayankLC93 2 жыл бұрын
such a Awesome Expalanation!!! TYSM!!
@rkalyankumar
@rkalyankumar 2 жыл бұрын
Best explanation of Morris traversals.
@shreyasnagabhushan4918
@shreyasnagabhushan4918 2 жыл бұрын
this was amazing bro, u made the topic look like a cake walk
@preetisahani5054
@preetisahani5054 Жыл бұрын
Great! Very nicely explained!
@iffatkhan5505
@iffatkhan5505 Жыл бұрын
couldnt find a code simpler than this on tbt....thanks a ton
@gandhijainamgunvantkumar6783
@gandhijainamgunvantkumar6783 2 жыл бұрын
Best explanation ever. bhaiya your explaining style is very very good.
@ooofrg4492
@ooofrg4492 4 ай бұрын
After a lots of struggle I got it 😂
@Aryan-fi2qf
@Aryan-fi2qf 2 жыл бұрын
I am getting Runtime error when trying run code without removing thread, We are moving right of curr node as we reach the rightmost of left subtree of curr node, so why do we need to remove the thread?
@divyansh2212
@divyansh2212 2 жыл бұрын
We need to remove the thread because the thread pointing to the curr node and this is changing the structure of the tree
@Abhishek-sn9jm
@Abhishek-sn9jm 10 ай бұрын
one day bunker will share competition with striver
@satyasaineelapala570
@satyasaineelapala570 8 ай бұрын
Amazing explanation
@pranjalck
@pranjalck 2 жыл бұрын
Thanks striver for making me feel like i could also think like this and approach like this... Thank u for Making me believe in myself 🙂❣️
@SohamDutta222
@SohamDutta222 5 ай бұрын
YOU ARE THE BEST! THANK YOU.
@aviadshiber6232
@aviadshiber6232 2 жыл бұрын
Damn, that is so smart technique. thanks for the great explanation!
@kaustubhdwivedi1729
@kaustubhdwivedi1729 2 жыл бұрын
Every time to rescue. Thank You !
@jaiminsolanki5478
@jaiminsolanki5478 2 жыл бұрын
Complex Concept made Simple, tysm Raj Bhaiya!!!
@dishant930
@dishant930 Жыл бұрын
Thank you so much Striver got it thoroughly!
@LuckyKumar-mt9km
@LuckyKumar-mt9km 4 ай бұрын
going left then going till the last right guy , that is called predecessor of current . It would have been more clearer if you had used that term in the explanation.
@sanyummanhas1994
@sanyummanhas1994 7 ай бұрын
at 4:26 what does he say a single kind of a Quadret??....i didnt quite get it, Can someone tell me what it is?
@ganeshkamath89
@ganeshkamath89 2 жыл бұрын
Thanks Striver. I didn't understand why space complexity became O(1)
@AkGautam_1904
@AkGautam_1904 2 жыл бұрын
Because we are just playing with pointers and not storing anything.
@saitejajollu2288
@saitejajollu2288 2 жыл бұрын
@@AkGautam_1904 it is clearly visible that he is adding the curr->val to the vector and returning the vector how can it be O(1).
@sumitkanth5349
@sumitkanth5349 Жыл бұрын
@@saitejajollu2288 vector space will we not counted because it was given in ques that we have to return a vector of inorder traversal
@anshumansinghrajawat7242
@anshumansinghrajawat7242 3 жыл бұрын
Amazing explanation , keep the good work going🙌
@huungryyyy
@huungryyyy 4 ай бұрын
😁😊thanku striver bhai
@karthik-varma-1579
@karthik-varma-1579 Ай бұрын
Watched 2times for Better Understanding.
@sharath5796
@sharath5796 2 жыл бұрын
Python Code: preorder=[] cur=root while cur: if cur.left == None: preorder.append(cur.val) cur=cur.right else: prev=cur.left while prev.right and prev.right!=cur: prev=prev.right if prev.right==None: prev.right=cur preorder.append(cur.val) cur=cur.left else: prev.right=None cur=cur.right return preorder
@vaalarivan_p
@vaalarivan_p 2 жыл бұрын
6:00 - 11:00 code walkthru: 14:10
@AmolGautam
@AmolGautam 11 ай бұрын
thank you for this explaination
@TheHorseRaddish
@TheHorseRaddish Жыл бұрын
Very clear explanation! Thanks!
@chirag_ccp
@chirag_ccp 2 жыл бұрын
Awesome Explanation Striver 🔥
@apmotivationakashparmar722
@apmotivationakashparmar722 19 күн бұрын
Can you please provide the Video Solution of remaining Questions in your A-2 Z Sheet ?
@shineinusa
@shineinusa 3 жыл бұрын
All I can say is thank you🙌
@kdoxfkck
@kdoxfkck 2 жыл бұрын
best explaination ever ever..... ❤️❤️🙂🙂
@anubhavpabby6856
@anubhavpabby6856 2 жыл бұрын
Thank you bhaiya, this video helped me to learn this concept quickly
@devils_mercy
@devils_mercy 6 ай бұрын
nice explanation
@SatyamKumar-bw4vi
@SatyamKumar-bw4vi 2 жыл бұрын
Hare Krishna...! Great Work
@siddwivedi9643
@siddwivedi9643 Жыл бұрын
nothing but respect !! Thank you bhaiya for this entire playlist
@rajivkumarkale
@rajivkumarkale 2 жыл бұрын
link should have been the correct term intead of thread as thread means something else. BTW good explaination.
@muskankalra2516
@muskankalra2516 3 жыл бұрын
Is Morris Traversal important for interviews?
@priyanshkumar17
@priyanshkumar17 8 ай бұрын
Yes, if the interviewer says to do some traversal in O(1) auxilliary space
@Anonymous____________A721
@Anonymous____________A721 3 ай бұрын
Yes Love babbar rejected in google interview for not saying Morris traversal
@sathwikabhignan5535
@sathwikabhignan5535 Жыл бұрын
fantastic explanation sir it really helped me a lot😀😀
@Yash-uk8ib
@Yash-uk8ib 3 жыл бұрын
sir, is it possible to do postorder with morris??
@deepthi4476
@deepthi4476 2 жыл бұрын
Can we do postorder also in the same way?
@shyamalaravind5906
@shyamalaravind5906 Жыл бұрын
Wonderful explanation. Please keep it up.
@ananthia5216
@ananthia5216 Жыл бұрын
Awesome❤❤❤🎉
@garvitanagpal
@garvitanagpal Жыл бұрын
You're pretty good at what you do.Keep going! :)
@KhushiVerma-y6t
@KhushiVerma-y6t 6 ай бұрын
You made tree easy to me 🙂
@ygpointoffun9418
@ygpointoffun9418 Жыл бұрын
what if striver we do not remove thread it will affect anymore??
@debjitmaji632
@debjitmaji632 2 жыл бұрын
Shouldn't the TC be 0(3N) .. N for whole traversal + N for making link + N for breaking link.. Plzz help!
@satyarajrana5702
@satyarajrana5702 4 ай бұрын
I don't understand how this is better than simple recursive preorder traversal? Because this approach also had O(N) space complexity right?
@SahilMathur-z1w
@SahilMathur-z1w 3 ай бұрын
o(1) we are using only a temppointer
@AyushSingh-em2il
@AyushSingh-em2il 6 ай бұрын
Amazing!
@mriduljain1981
@mriduljain1981 Жыл бұрын
completed lecture 37 of free ka tree series.
@sudarshan3901
@sudarshan3901 2 жыл бұрын
Amazing explanation !!! Loved it ...
L38. Flatten a Binary Tree to Linked List | 3 Approaches | C++ | Java
21:51
كم بصير عمركم عام ٢٠٢٥😍 #shorts #hasanandnour
00:27
hasan and nour shorts
Рет қаралды 8 МЛН
Мама у нас строгая
00:20
VAVAN
Рет қаралды 10 МЛН
World’s strongest WOMAN vs regular GIRLS
00:56
A4
Рет қаралды 51 МЛН
Arpit Bhayani talks about real engineering for 1 hour straight
1:16:23
Python vs Rust vs C++ Speed Comparison
1:50
Mial Skywalker
Рет қаралды 115 М.
LeetCode was HARD until I Learned these 15 Patterns
13:00
Ashish Pratap Singh
Рет қаралды 577 М.
Learning C# In A Week... Otherwise I Fail University
9:04
Elliptic Curve Cryptography Overview
11:29
F5 DevCentral
Рет қаралды 477 М.
70 Leetcode problems in 5+ hours (every data structure) (full tutorial)
5:27:20