Iterative & Recursive - Binary Tree Inorder Traversal - Leetcode 94 - Python

  Рет қаралды 104,615

NeetCode

NeetCode

Күн бұрын

Пікірлер: 73
@jabir5768
@jabir5768 2 жыл бұрын
Thank you father
@ancai5498
@ancai5498 11 ай бұрын
I think the core idea to grasp the iterative solution is to understand the recursive solution first, Basically, when you do the dfs(node->left), it just keeps finding its leftmost node until it doesn't, then you'll have to pop the stack to 'visit' the node(in-order), then dfs(node->right), it's the time for its right node. Hope it helps.
@MaheshKumar-qq7ts
@MaheshKumar-qq7ts 2 жыл бұрын
I was searching for this explanation few hours back and here it is. You are doing a great job and I mean literally "great job".
@MaheshKumar-qq7ts
@MaheshKumar-qq7ts 2 жыл бұрын
Also I was worried that since you got job now video would not be that frequent, Please prove us wrong.
@muneebmohd
@muneebmohd Жыл бұрын
You have literally every single question explained, jesus. Thank you man I love you!
@thevinarokiarajl2149
@thevinarokiarajl2149 Жыл бұрын
I am as noobish as they come and I first check for any neetcode vids for a problem I encounter in LC. Ur goated fr
@pruthvinarayana9568
@pruthvinarayana9568 2 ай бұрын
I love the way he explains Easy problems indetailed keping in mind of beginners 💖
@gauravsharma3829
@gauravsharma3829 2 жыл бұрын
Thanks for explaining so well! It was so clear that I understood in one go.
@trbhlk
@trbhlk 6 ай бұрын
You should add the call stack explanation to your recursion section from the DS & Algos course. This was insanely helpful to understand recursion.
@AparnaBushan
@AparnaBushan Жыл бұрын
I guess instead of inner while Ioop, we could simply use if condition , am I right ? def inorder(root): curr, stack = root, [] res = [] while curr or stack: if curr: stack.append(curr) curr = curr.left else: curr = stack.pop() res.append(curr) curr = curr.right return res
@mr.mrs.gutierrez4975
@mr.mrs.gutierrez4975 2 жыл бұрын
Why I need to define private function “inorder”? Why I cannot directly recur self.inordertraversal(root.left) and self.inorderyraversal(root.right)?
@HiHi-xl9kn
@HiHi-xl9kn 2 жыл бұрын
Glad to see i'm not the only one that always mistakenly append the node to res lol. Could you share what you use to write/draw? ipad? tablet? touch screen?
@cewenchi
@cewenchi 6 ай бұрын
Only moment that i realized the importance of the tree structure with a stack is preparing jobs interview ...
@andreytamelo1183
@andreytamelo1183 2 жыл бұрын
Thanks!
@firstacc5442
@firstacc5442 Жыл бұрын
can you please explain Morris Traversal algorithm, please.
@bossmusa9075
@bossmusa9075 Жыл бұрын
thank you after your explanation i can do it on my own, the description was afwul
@servantofthelord8147
@servantofthelord8147 4 ай бұрын
The iterative code is hard for me to wrap my head around, but I'll keep revisiting it. For anyone in the same boat, I found this code on GeeksforGeeks and it helped me to understand it even more: cur = root stack = [] res = [] while True: if cur: stack.append(cur) cur = cur.left elif stack: cur = stack.pop() res.append(cur.val) cur = cur.right else: return res
@Baraa_Sy_99
@Baraa_Sy_99 4 ай бұрын
Thanks man, that was really helpful 🌷
@servantofthelord8147
@servantofthelord8147 4 ай бұрын
@@Baraa_Sy_99 Glad it helped you!
@amitp277
@amitp277 2 жыл бұрын
great explanation thanks!
@Goodday-nm7zp
@Goodday-nm7zp Жыл бұрын
this channel has helped me so much, thank you!
@ganeshbirajdar4382
@ganeshbirajdar4382 2 жыл бұрын
Thanks, very helpful 👍
@Chestuhr
@Chestuhr 3 ай бұрын
Recursive took like 2 minutes but for some odd reason the iterative solution was trickier than medium graph problems
@gunahawk6893
@gunahawk6893 2 жыл бұрын
dude best explanation
@quirkyquester
@quirkyquester 5 ай бұрын
best video brother! love this!
@richardmunyao4607
@richardmunyao4607 2 жыл бұрын
Thanks so much! This helped a bunch!
@arjunhamdalah4139
@arjunhamdalah4139 2 жыл бұрын
Do you have basic cs leetcode Playlist? Thanks
@yogeshmishra1726
@yogeshmishra1726 2 жыл бұрын
Do we need to know about iterative method if we know recursive one
@akira_asahi
@akira_asahi 2 жыл бұрын
Thank you for the video. I am grateful for your time and contribution. Kind regards, Akira.
@tanyashankar9966
@tanyashankar9966 Жыл бұрын
May I know why only stack is considered hare ??
@Marcox385
@Marcox385 2 жыл бұрын
Totally off-topic but how you got so good at drawing instructions? Lately I've been giving tutoring and I took inspiration from you (yet again) and used paint 3d for online sessions, the thing is that I always take too long to write/draw stuff that doesn't even looks that good. So, is it just practice or you have a distinct device other than a mouse?
@olalekansogunle
@olalekansogunle 2 жыл бұрын
@NeetCode I have this exact question. Please help if you are able to get to answering
@rikpatel
@rikpatel 2 жыл бұрын
He has responded to this before in another video. He uses Paint 3D and mouse. I guess its just practice. You can alternatively use Drawing pad
@halahmilksheikh
@halahmilksheikh 2 жыл бұрын
@@rikpatel He uses a mouse??? That's crazy. He writes like he uses a stylus.
@St0n3dCold
@St0n3dCold 2 жыл бұрын
just use a stylus
@Marcox385
@Marcox385 2 жыл бұрын
@@St0n3dCold lmao yeah why I didn't thought that? Let me also convert my display into a touchscreen. Better yet, let me get an expensive graphic tablet for something that isn't my profession, that way I could use an stylus, or several, to enhance the tutoring experience
@pawelkorsunskiy9660
@pawelkorsunskiy9660 2 жыл бұрын
You are a great teacher!
@lucy2003-d8p
@lucy2003-d8p Жыл бұрын
heyyy brooo thanks alot❤
@alexgolomb363
@alexgolomb363 10 ай бұрын
Thank you.
@lingyuhu4623
@lingyuhu4623 2 жыл бұрын
Hope to have your video of 145. Binary Tree Postorder Traversal
@vdyb745
@vdyb745 2 жыл бұрын
I can't thank you enough for your videos !!!
@leetcode650
@leetcode650 Жыл бұрын
why are we appending the node to stack instead of just appending the values.
@dreamphoenix
@dreamphoenix Жыл бұрын
This is fantastic, thank you.
@trenaesthetics2139
@trenaesthetics2139 Жыл бұрын
Bro I love u thanks man
@andreypopov6166
@andreypopov6166 8 ай бұрын
easy :) easy to remember...
@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
@RelaxationVideos99
@RelaxationVideos99 2 жыл бұрын
Awesome Thanks!
@anonymoussloth6687
@anonymoussloth6687 2 жыл бұрын
Please do shortest path visiting all nodes on leetcode. It's a hard problem.
@ziiiiiiiiiii7325
@ziiiiiiiiiii7325 Жыл бұрын
can somebody help me out here: def inorderTraversal(self, root: Optional[TreeNode]) -> List[int]: result = [] stack = [] cur = root while cur or stack: while cur: stack.append(cur)
@NaveenKumar-xq6ce
@NaveenKumar-xq6ce Жыл бұрын
Because a node is a object of class TreeNode. Makes sense to store in stack cause it stores other things like functions and stuff. But we need to store values in array
@nhbknhb
@nhbknhb 2 жыл бұрын
Any vlogs coming?
@kaikim8402
@kaikim8402 2 жыл бұрын
Is it Okay if u are solving this problem for the first time and ended up looking at the solution like this? I feel guilty seeing the solution that i will be looking for solution every time i encounter problems that i can't solve. Any advice?
@erinwolf1563
@erinwolf1563 Жыл бұрын
I'm currently in the same shoes as u right now.. but based on my research I think it's okay.. don't feel guilty
@antoinenijhuis450
@antoinenijhuis450 10 ай бұрын
Where are you at in your programming/comp science journey? @@erinwolf1563 @kaikim8402
@nitrozeus5458
@nitrozeus5458 Жыл бұрын
Yer a wizard, Harry
@Cubeone11
@Cubeone11 2 ай бұрын
I understood what is happening but its very hard for me to understand why thats happening
@masternobody1896
@masternobody1896 2 жыл бұрын
Nice
@numberonep5404
@numberonep5404 2 жыл бұрын
I don't think the link you put in the description is the right one :p nice video btw
@vdyb745
@vdyb745 2 жыл бұрын
True leads to 108 instead of 94
@NeetCode
@NeetCode 2 жыл бұрын
fixed!
@tsunningwah3471
@tsunningwah3471 7 ай бұрын
zhin
@bufdud4
@bufdud4 Жыл бұрын
This iterative solution is pretty unintuitive.
@СтасюкАндрій
@СтасюкАндрій Жыл бұрын
yea i solved this problem iteratively with a hint of stack usage and it took me almost 5 hours.... my self-esteem is low now lol
@antoinenijhuis450
@antoinenijhuis450 10 ай бұрын
Where are you at in your programming/comp science journey tho? @@СтасюкАндрій
@tsunningwah3471
@tsunningwah3471 7 ай бұрын
b
@tsunningwah3471
@tsunningwah3471 7 ай бұрын
zhina
@trbhlk
@trbhlk 6 ай бұрын
You should add the call stack explanation to the recursion section in your DS & Algos course. This was insanely helpful to understand recursion and draw a connection to stacks. Thank you.
@LilJollyJoker
@LilJollyJoker 7 ай бұрын
Thank You!
Next Greater Element I - Leetcode 496 - Python
14:53
NeetCode
Рет қаралды 79 М.
Amazing remote control#devil  #lilith #funny #shorts
00:30
Devil Lilith
Рет қаралды 16 МЛН
Молодой боец приземлил легенду!
01:02
МИНУС БАЛЛ
Рет қаралды 1,8 МЛН
Увеличили моцареллу для @Lorenzo.bagnati
00:48
Кушать Хочу
Рет қаралды 8 МЛН
Dynamic Programming isn't too hard. You just don't know what it is.
22:31
DecodingIntuition
Рет қаралды 196 М.
Binary Search Tree Iterator - Leetcode 173 - Python
12:47
NeetCode
Рет қаралды 42 М.
LeetCode was HARD until I Learned these 15 Patterns
13:00
Ashish Pratap Singh
Рет қаралды 576 М.
L10. iterative Inorder Traversal in Binary Tree | C++ | Java | Stack
11:14
How I would learn Leetcode if I could start over
18:03
NeetCodeIO
Рет қаралды 686 М.
💁‍♂️Зачем infinix это сделала?🤦‍♂️
0:42
Глюк на моем iPhone 16 Pro Max
0:26
ТЕХНОБЛОГ ГУБАРЕВ СЕРГЕЙ
Рет қаралды 1,4 МЛН
Review Máy Đếm Tiền Tính Tổng #shorts
0:26
Review Máy Đếm Tiền
Рет қаралды 5 МЛН
Apple Intelligence vs Galaxy Ai✨ #Shorts
0:19
lifegoodd
Рет қаралды 4,3 МЛН
Apple ВАС ОБМАНЫВАЕТ! #smartphone #айфон #интересное
0:53
ТЕХНОБЛОГ АЛИША
Рет қаралды 143 М.