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

  Рет қаралды 230,242

take U forward

take U forward

2 жыл бұрын

Entire DSA Course: takeuforward.org/strivers-a2z...
Check our Website:
Linkedin/Instagram/Telegram: linktr.ee/takeUforward
#treeSeries #striver #placements

Пікірлер: 271
@takeUforward
@takeUforward 2 жыл бұрын
Please likeeee, shareeee and subscribeeeeeeee :) Also follow me at Insta: Striver_79 Aaaye hi ho, toh series bhi dekh lo ;)
@user-xi5sm3dz9q
@user-xi5sm3dz9q 2 жыл бұрын
Bilkul.
@deepaksarvepalli2344
@deepaksarvepalli2344 2 жыл бұрын
Just informed to my friends that video is out 😁
@PrashantSingh-ej8lz
@PrashantSingh-ej8lz 2 жыл бұрын
Hey striver please add morris postorder traversal
@priyanshkumar_iitd
@priyanshkumar_iitd 5 ай бұрын
Wonderful explanation bhaiya...
@uRamPlus
@uRamPlus 2 жыл бұрын
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 Жыл бұрын
In 3rd case shouldn't it be Remove the thread ..and THEN PRINT NODE AND GO RIGHT??
@parthsalat
@parthsalat Жыл бұрын
@@AbhishekYadav-rm4hw Yes, right. You can add it to 'your' notes
@sparshsharma6068
@sparshsharma6068 2 жыл бұрын
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 9 ай бұрын
can you please explain the intuition of connecting leftmost node of right subtree to cur
@iamnoob7593
@iamnoob7593 20 күн бұрын
Just replace left with right and right with left of preorder code. Thanks
@slowedReverbJunction
@slowedReverbJunction 2 жыл бұрын
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 2 жыл бұрын
Bro kidda? 2nd year ki 3rd.
@akashgupta13
@akashgupta13 2 жыл бұрын
@@karanveersingh5535 😂
@parthsalat
@parthsalat Жыл бұрын
@@akashgupta13 😂
@anmolkohli6299
@anmolkohli6299 2 жыл бұрын
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. 🔊
@priyanshkumar_iitd
@priyanshkumar_iitd 5 ай бұрын
Yeah, you're right.@@CostaKazistov
@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 8 ай бұрын
Every time I watch it, things become more clearer. This is the third time in 6 months.
@harshanand4501
@harshanand4501 2 жыл бұрын
I studied it from book but was unable to understand it. But after watching your video understood it completely. Great Explanation!
@amanbhadani8840
@amanbhadani8840 2 жыл бұрын
This is indeed the best explanation of this series, truly crystal clear.Kudos to your work for the programming community Bhaiya.
@deepanshukhanna1780
@deepanshukhanna1780 2 жыл бұрын
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 2 жыл бұрын
Right sir ❤️
@vivekjaiswar6825
@vivekjaiswar6825 2 жыл бұрын
They are more interested in "How to get into google" type of videos.
@01_abhijeet49
@01_abhijeet49 Жыл бұрын
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 13 күн бұрын
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.
@abhijeetmishra3804
@abhijeetmishra3804 9 ай бұрын
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 ...
@paryt7696
@paryt7696 2 жыл бұрын
1 upvote, best explaination brother, and congo for the offers from facebook and google, this news had definitely shut the mouth of haters
@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!
@deepakjain4481
@deepakjain4481 9 ай бұрын
you are best man no one even touch these type of topics
@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 15 күн бұрын
You did?
@natanshisharma8828
@natanshisharma8828 12 күн бұрын
@@073.iyasmeenmahilang3 lol
@aryanchaurasia1081
@aryanchaurasia1081 Жыл бұрын
Amazingly explained ,the intuition has just fit into my head forever❤️
@mohitsingh7793
@mohitsingh7793 2 жыл бұрын
Crystal and Clear Explanation for such a difficult topic❤
@santanu29
@santanu29 Жыл бұрын
Great Explanation. A bit of a heavy topic, takes time to wrap your head around. Will revisit after a week.
@preetkatiyar969
@preetkatiyar969 2 жыл бұрын
Best explanation. No one can explain better than this way.
@umber3117
@umber3117 7 ай бұрын
Best explanation.Thanks a lot for making these videos.
@anshumanprasad5410
@anshumanprasad5410 2 жыл бұрын
thank you so much after struggling so much on this topic, I watched your video.
@saisardesai5548
@saisardesai5548 23 күн бұрын
never seen an explaination like this wonderful!
@preetisahani5054
@preetisahani5054 10 ай бұрын
Great! Very nicely explained!
@iamnoob7593
@iamnoob7593 20 күн бұрын
Superb striver , Man ur excellent
@shreyashachoudhary480
@shreyashachoudhary480 2 жыл бұрын
Really amazing explanation. Thanks. Though that AMORTIZED time complexity confuses a bit.
@mayankdham8917
@mayankdham8917 2 жыл бұрын
Best explanation of morris traversal..
@stith_pragya
@stith_pragya 9 ай бұрын
Thank You So Much for this wonderful video............🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻
@SohamDutta222
@SohamDutta222 2 ай бұрын
YOU ARE THE BEST! THANK YOU.
@kashishsharma6809
@kashishsharma6809 2 жыл бұрын
Application of this algo = change links in tree ques. Eg flatten the tree ( next ques of this playlist). 👍👍
@surabhichoubey2987
@surabhichoubey2987 2 жыл бұрын
Literally it was seems Soo horrible now it's seems so easy just because of you thanks a lot ❤️
@bharatht5595
@bharatht5595 2 жыл бұрын
Clear explanation from scratch. Thanks
@nagasrikuncharapu3736
@nagasrikuncharapu3736 Ай бұрын
It took time to understand this, but it's worth it.
@devanshtiwari9163
@devanshtiwari9163 11 ай бұрын
Great Explanation till now for Morris Traversal.
@sumitgupta310
@sumitgupta310 2 ай бұрын
what a amazing Explaination man!!
@AmolGautam
@AmolGautam 8 ай бұрын
thank you for this explaination
@anmolswarnkar7707
@anmolswarnkar7707 2 жыл бұрын
This was very well explained, thanks a lot.
@Flash-qr5oh
@Flash-qr5oh 8 ай бұрын
he really made the code easy....
@jatinsalve364
@jatinsalve364 5 ай бұрын
Interviewer will ask this question like , do the traversal without using recursion or-any extraspace.
@gandhijainamgunvantkumar6783
@gandhijainamgunvantkumar6783 2 жыл бұрын
Best explanation ever. bhaiya your explaining style is very very good.
@aviadshiber6232
@aviadshiber6232 2 жыл бұрын
Damn, that is so smart technique. thanks for the great explanation!
@amanali9501
@amanali9501 Жыл бұрын
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
@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
@rkalyankumar
@rkalyankumar Жыл бұрын
Best explanation of Morris traversals.
@Ktr_792
@Ktr_792 3 ай бұрын
Nothing Better Than this explanation
@anshumansinghrajawat7242
@anshumansinghrajawat7242 2 жыл бұрын
Amazing explanation , keep the good work going🙌
@satyasaineelapala570
@satyasaineelapala570 4 ай бұрын
Amazing explanation
@shreyasnagabhushan4918
@shreyasnagabhushan4918 Жыл бұрын
this was amazing bro, u made the topic look like a cake walk
@VinodKumar-by4pt
@VinodKumar-by4pt Жыл бұрын
One of the easiest explanation ever, Thank you strive Bhaiya❤
@iffatkhan5505
@iffatkhan5505 Жыл бұрын
couldnt find a code simpler than this on tbt....thanks a ton
@harshdeepak3281
@harshdeepak3281 2 жыл бұрын
great explanation!!! loving the tree series!!!
@garvitanagpal
@garvitanagpal Жыл бұрын
You're pretty good at what you do.Keep going! :)
@MayankLC93
@MayankLC93 2 жыл бұрын
such a Awesome Expalanation!!! TYSM!!
@aviralgoel5709
@aviralgoel5709 Жыл бұрын
Actual video starts @2:10
@AyushSingh-em2il
@AyushSingh-em2il 3 ай бұрын
Amazing!
@jaiminsolanki5478
@jaiminsolanki5478 2 жыл бұрын
Complex Concept made Simple, tysm Raj Bhaiya!!!
@ordinaryhomosapien1181
@ordinaryhomosapien1181 2 жыл бұрын
Such a wonderful Explanation
@anubhavpabby6856
@anubhavpabby6856 2 жыл бұрын
Thank you bhaiya, this video helped me to learn this concept quickly
@devils_mercy
@devils_mercy 2 ай бұрын
nice explanation
@TheHorseRaddish
@TheHorseRaddish Жыл бұрын
Very clear explanation! Thanks!
@morhadi
@morhadi 2 ай бұрын
The dopamine rush after your code runs on first try >>>
@huungryyyy
@huungryyyy 29 күн бұрын
😁😊thanku striver bhai
@sudarshan3901
@sudarshan3901 2 жыл бұрын
Amazing explanation !!! Loved 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
@user-tk2vg5jt3l
@user-tk2vg5jt3l 4 ай бұрын
Thank you Bhaiya
@dishant930
@dishant930 Жыл бұрын
Thank you so much Striver got it thoroughly!
@ananthia5216
@ananthia5216 10 ай бұрын
Awesome❤❤❤🎉
@debjitmaji632
@debjitmaji632 Жыл бұрын
Shouldn't the TC be 0(3N) .. N for whole traversal + N for making link + N for breaking link.. Plzz help!
@ooofrg4492
@ooofrg4492 15 күн бұрын
After a lots of struggle I got it 😂
@kdoxfkck
@kdoxfkck 2 жыл бұрын
best explaination ever ever..... ❤️❤️🙂🙂
@Yash-uk8ib
@Yash-uk8ib 2 жыл бұрын
sir, is it possible to do postorder with morris??
@shyamalaravind5906
@shyamalaravind5906 Жыл бұрын
Wonderful explanation. Please keep it up.
@SreeCharan-dx7oc
@SreeCharan-dx7oc 6 ай бұрын
THANK YOU
@ygpointoffun9418
@ygpointoffun9418 11 ай бұрын
what if striver we do not remove thread it will affect anymore??
@sathwikabhignan5535
@sathwikabhignan5535 Жыл бұрын
fantastic explanation sir it really helped me a lot😀😀
@KartikeyTT
@KartikeyTT 2 ай бұрын
ty sir
@kaustubhdwivedi1729
@kaustubhdwivedi1729 Жыл бұрын
Every time to rescue. Thank You !
@rohitraj9515
@rohitraj9515 Ай бұрын
does morris traversal is also for postorder?
@sanyummanhas1994
@sanyummanhas1994 3 ай бұрын
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?
@pankajvishwakarma3124
@pankajvishwakarma3124 27 күн бұрын
✅ 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; }
@glebbondarenko67
@glebbondarenko67 Жыл бұрын
Thank you man I got it. It was a good explanation
@ganeshkamath89
@ganeshkamath89 2 жыл бұрын
Thanks Striver. I didn't understand why space complexity became O(1)
@AvinashKumar-pb2op
@AvinashKumar-pb2op 2 жыл бұрын
Because we are just playing with pointers and not storing anything.
@saitejajollu2288
@saitejajollu2288 Жыл бұрын
@@AvinashKumar-pb2op 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
@anmolsingh3482
@anmolsingh3482 2 жыл бұрын
Wonderful explanation Sir!
@vaalarivan_p
@vaalarivan_p Жыл бұрын
6:00 - 11:00 code walkthru: 14:10
@KBSiddhantKhanna
@KBSiddhantKhanna 2 жыл бұрын
Excellent Lecture
@chirag_ccp
@chirag_ccp 2 жыл бұрын
Awesome Explanation Striver 🔥
@prakharagarwal8448
@prakharagarwal8448 2 жыл бұрын
loved the explanation !!
@rishichowdhury4296
@rishichowdhury4296 8 ай бұрын
can we do post order using morris traversal?
@vidhijain5110
@vidhijain5110 3 ай бұрын
why we use cur as an object in line no.60
@Gaurav-vl6ym
@Gaurav-vl6ym 10 ай бұрын
thank you sir
@deepthi4476
@deepthi4476 2 жыл бұрын
Can we do postorder also in the same way?
@vyomgoyal3125
@vyomgoyal3125 4 ай бұрын
Can Postorder also be done using Morris Traversal?
@Abhishek-sn9jm
@Abhishek-sn9jm 7 ай бұрын
one day bunker will share competition with striver
@satvrii
@satvrii Жыл бұрын
Sucha a beautiful explanation ❤
@tejassrivastava6971
@tejassrivastava6971 2 жыл бұрын
nicely explained !
@RishabhKumarRoyVIT
@RishabhKumarRoyVIT Ай бұрын
is is important from placement pov?
@shashankojha3452
@shashankojha3452 2 жыл бұрын
Great Explanation
@shineinusa
@shineinusa 2 жыл бұрын
All I can say is thank you🙌
@satwikvarma2804
@satwikvarma2804 2 жыл бұрын
Wow, simply amazing
@studshelper9901
@studshelper9901 2 ай бұрын
preorder : we must push curr->val in preorder before visiting left inorder: we will visit left then push the curr->val
@namanshah2688
@namanshah2688 2 жыл бұрын
very well explained thankyou bro...
L38. Flatten a Binary Tree to Linked List | 3 Approaches | C++ | Java
21:51
Morris Inorder Tree Traversal
11:44
Tushar Roy - Coding Made Simple
Рет қаралды 144 М.
WORLD'S SHORTEST WOMAN
00:58
Stokes Twins
Рет қаралды 128 МЛН
Best KFC Homemade For My Son #cooking #shorts
00:58
BANKII
Рет қаралды 73 МЛН
Survive 100 Days In Nuclear Bunker, Win $500,000
32:21
MrBeast
Рет қаралды 107 МЛН
Why Is He Unhappy…?
00:26
Alan Chikin Chow
Рет қаралды 69 МЛН
I gave 127 interviews. Top 5 Algorithms they asked me.
8:36
Sahil & Sarra
Рет қаралды 633 М.
Learning C# In A Week... Otherwise I Fail University
9:04
Should you grind LeetCode? feat. NeetCode | 051
56:22
Backend Banter
Рет қаралды 47 М.
Why Sentinels Got Worse but GEN.G Got Better
12:07
Platoon
Рет қаралды 93 М.
8 patterns to solve 80% Leetcode problems
7:30
Sahil & Sarra
Рет қаралды 290 М.
Binary Tree Inorder Traversal | Morris Traversal | Leetcode-94
15:14
codestorywithMIK
Рет қаралды 4,5 М.
Big Update in Java for Learners and Trainers
0:59
Telusko
Рет қаралды 375 М.
L17. Maximum Path Sum in Binary Tree | C++ | Java
17:50
take U forward
Рет қаралды 314 М.
WORLD'S SHORTEST WOMAN
00:58
Stokes Twins
Рет қаралды 128 МЛН