LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal (Algorithm Explained)

  Рет қаралды 80,869

Nick White

Nick White

Күн бұрын

The Best Place To Learn Anything Coding Related - bit.ly/3MFZLIZ
Join my free exclusive community built to empower programmers! - www.skool.com/...
Preparing For Your Coding Interviews? Use These Resources
--------------------
(My Course) Data Structures & Algorithms for Coding Interviews - thedailybyte.d...
AlgoCademy - algocademy.com...
Daily Coding Interview Questions - bit.ly/3xw1Sqz
10% Off Of The Best Web Hosting! - hostinger.com/...
Follow Me on X/Twitter - x.com/nickwhit...
Follow My Instagram - / nickwwhite
Other Social Media
----------------------------------------------
Discord - / discord
Twitch - / nickwhitettv
TikTok - / nickwhitetiktok
LinkedIn - / nicholas-w-white
Show Support
------------------------------------------------------------------------------
Patreon - / nick_white
PayPal - paypal.me/nick....
Become A Member - / @nickwhite
#coding #programming #softwareengineering

Пікірлер: 83
@ArjunKalidas
@ArjunKalidas 4 жыл бұрын
Most of this explanation just bounced over my head! Hey Nick, can you please explain the algorithm a bit more clearly, before getting into the code. Because end of the day, logic and derivation of the solution is what matters right, and coding will be easy after that! Thanks for the video though.
@SaurabhYadav-wj7zm
@SaurabhYadav-wj7zm 4 жыл бұрын
I think he just see the code and makes the video, without even getting into the algorithm part.
@malikwaseem1983
@malikwaseem1983 3 жыл бұрын
Algo in the above link follows natural recursive order and thus is easy to understand and makes more sense
@raj-nq8ke
@raj-nq8ke 2 жыл бұрын
You can first go through theory videos and then come here to watch code explanation.
@Aditya-rm3bl
@Aditya-rm3bl 3 жыл бұрын
Not sure how he has so much followers. The solutions are either taken from solution or reporduced from memory without any explanations.
@MrSaurus
@MrSaurus Жыл бұрын
He's good at explaining the problems that don't involve trees, but yeah I don't like watching his tree tutorials
@trung.n
@trung.n 3 жыл бұрын
You can convert the in order array to a hash map storing {inorder val : index} to avoid looping through the in order array each time
@zmanneverdie
@zmanneverdie 4 жыл бұрын
I find that the main time cost in this solution is from the for loop search part, you can reduce the time cost by using a hashmap for inorder element search.
@AkshayKumar-xh2ob
@AkshayKumar-xh2ob 3 жыл бұрын
Your solutions always explain the intuition behind the solution.
@vinothborn2win
@vinothborn2win 2 жыл бұрын
Thanks Nick. You are awesome! Constructing Binary Tree from PostOrder and Inorder is very similar to this problem. In PostOrder, the sequence is "left, right, center", which means we need to loop PostOrder array from the end. Recursive inputs to root.left and root.right will also differ accordingly (but very similar to this video).
@rajman617
@rajman617 2 жыл бұрын
Yes I can understand for root.left It will be “postEnd-1” but can you say like Wat it would be for root.right ....
@saiyamkumarsingh619
@saiyamkumarsingh619 4 жыл бұрын
So finally you did this problem. I watched yesterday's live stream when you were solving this.
@kittwwang
@kittwwang 3 жыл бұрын
Your solution is always very concise. I was coding it and got all the index mangled up. Thanks for the video!
@brovet78
@brovet78 4 жыл бұрын
Can you please also tell us the time and space complexity when making these video? Just subscribed. Yours is easier to understand than solution in discussion lol
@trinhta9410
@trinhta9410 4 жыл бұрын
I think the time complexity is O(N) and space complexity is O(N) if you count stack frame of the recursion.
@arsenohanyan306
@arsenohanyan306 4 жыл бұрын
@@trinhta9410 They are both O(n^2)
@beyondlimits8159
@beyondlimits8159 2 жыл бұрын
@@arsenohanyan306 how?
@stith_pragya
@stith_pragya 9 ай бұрын
Thank You So Much for this wonderful video.......🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻
@Hgh38
@Hgh38 3 жыл бұрын
Anyone think question is hard? I even saw this video and I still don’t get it.
@FitnessChaos
@FitnessChaos 3 жыл бұрын
Good explanation of preorder, inorder and postorder
@nikakondra5321
@nikakondra5321 Жыл бұрын
Thank you Nick!
@User_Unknown_Anonymous
@User_Unknown_Anonymous 4 жыл бұрын
Just subscribed cause you said you can grow as you get more subscribers LOL We will help your channel grow, you help us learn data structures and algorithms. I appreciate you teaching us these concepts.
@lowkey_anp
@lowkey_anp 4 жыл бұрын
so u knoe how to reverse a linked list, but do u knoe how to design a large scale software system
@khandelwalsahaj36
@khandelwalsahaj36 4 жыл бұрын
yes
@suyashsorte
@suyashsorte 4 жыл бұрын
Great video!!! I took a while to understand the concept. Can you tell what is the time and space complexity for this approach?
@rasikachavan7290
@rasikachavan7290 4 жыл бұрын
Thanks for simplifying the problem!! Can you tell time complexity and space complexity of this?
@LtMewS
@LtMewS Жыл бұрын
time complexity looks like n^2 as looping to find inIndex and doing recursion takes o(n) and inside it we r looping with o(n) so its o(n^2)
@user-mi8ew2to8e
@user-mi8ew2to8e 4 жыл бұрын
I didn't know Nelson Bighetti aka 'Big Head' has a youtube channel
@killingjoyandmemeing
@killingjoyandmemeing 3 жыл бұрын
lol
@user-vu8jp3si2r
@user-vu8jp3si2r Жыл бұрын
Why code in this video get stack over flow?
@arianphilips5777
@arianphilips5777 4 жыл бұрын
It took me 1 hour to do this problem, and you just did it in 11 minutes DAM
@sumandutta83
@sumandutta83 4 жыл бұрын
there was a leetcode pre-existing solution :-)
@yashsvidixit7169
@yashsvidixit7169 3 жыл бұрын
And I can't understand it for a month now, FML
@sohamharnale2878
@sohamharnale2878 4 жыл бұрын
Do you solve problems live on Twitch? Would love to be a part of it!
@_sf_editz1870
@_sf_editz1870 8 ай бұрын
Coolll easiest explanation on internet
@emansrnme1371
@emansrnme1371 4 жыл бұрын
If I am a novice to trees, what can you recommend? I couldn't understand the video.
@chiranjeevipippalla4483
@chiranjeevipippalla4483 4 жыл бұрын
I would recommend you to watch the videos of mycodeschool for basics of trees. They are very clearly explained. Once you get an idea of those concepts, watch these videos. You will find this easy.
@emansrnme1371
@emansrnme1371 4 жыл бұрын
I understand what are trees now, can write a search and insert, but still don't understand this video
@ramchandrayadav8378
@ramchandrayadav8378 4 жыл бұрын
@@emansrnme1371 You should try by yourself. I solved this problem by myself without using hashing. I am also a novice . I came here to learn a different approach for solving this problem, using hashing which i couldn't understand .
@rajman617
@rajman617 2 жыл бұрын
@@emansrnme1371 keep learning and solving these kind of problems . Trust me you will start learning as you keep practising. In the start it looks “BLANK” but as the days go ... we start understanding each and every bit .
@trinhta9410
@trinhta9410 4 жыл бұрын
it is hard to understand the formula for the prestart of the right nodes. My solution was passing prestart as reference and increment its by 1 for every-time a node is created. Thank you for your video.
@darod6098
@darod6098 4 жыл бұрын
Thanks for the video, it helped me. Do you know what would be the approach if the array would have repeated elements? (or the work around)
@lazycoder1910
@lazycoder1910 3 жыл бұрын
Thanks for all ur work!
@evgeni-nabokov
@evgeni-nabokov 4 жыл бұрын
Is there a way to improve look up for the inIndex?
@anirudhatalmale5575
@anirudhatalmale5575 4 жыл бұрын
Awesome explanation. Thanks
@user-bi4gg9he7v
@user-bi4gg9he7v 4 жыл бұрын
But in other sites like gfg they are passing preStart as prestart+1 in both root->left and root->right and still giving correct output. Can you explain why?
@SnowFlameSupremacy
@SnowFlameSupremacy 2 жыл бұрын
I'm late but thats because they are using global variable... the method is good but the problem is leetcode doesn't execute it right for some reason ....meaning it works when ur executing urself for test cases but when submitting it gives wrong answer
@SnowFlameSupremacy
@SnowFlameSupremacy 2 жыл бұрын
I'm late but thats because they are using global variable... the method is good but the problem is leetcode doesn't execute it right for some reason ....meaning it works when ur executing urself for test cases but when submitting it gives wrong answer
@calfland79
@calfland79 4 жыл бұрын
In line 29, where does inIndex - inStart + 1 come from? In other words, how do you map inorder index to preorder index?
@RajatGautamism
@RajatGautamism 4 жыл бұрын
he is just skipping number of element which are supposed to be on the left subtree, from looking at number of elements in inorder array.If you didn't get this then go through recursion concepts again.
@joanperezlozano7405
@joanperezlozano7405 2 жыл бұрын
This might be a late response, but perhaps useful for newcomers... in addition to what Rajat said, this is sort of like a small formula to count in a set of consecutive numbers (in this case our set is the indexes) so we count from one position to another (greater number - smaller number) and then you + 1 to make an inclusive count or -1 to make an exclusive count. E.g try with counting numbers from 3 to 6 inclusive, for the set: [0, 1, 2, 3, 4, 5, 6, 7] -> 6 - 3 + 1 = 4. Same concept is applied to know how many indexes we want to skip in the preorder array to build the right subtree.
@rodanm
@rodanm 3 жыл бұрын
Nick, can you share your 2ms submission? Thanks
@MrThepratik
@MrThepratik 4 жыл бұрын
nailed it . thanks for the clear explaination
@adamberry7536
@adamberry7536 4 жыл бұрын
You can save the value and index lookup for the inorder in a hashmap to avoid the for loop in your recursive stack.
@cathywei8842
@cathywei8842 3 жыл бұрын
Thank you ! really helpful!
@vasanthakumarn7196
@vasanthakumarn7196 4 жыл бұрын
Hii bro can u solve HOUSE ROBBERY 3 problem ? Under tree category
@ShubhamVerma-cn7bc
@ShubhamVerma-cn7bc 4 жыл бұрын
Nice Video, gr8 explaination
@sophiezhang6516
@sophiezhang6516 4 жыл бұрын
good explaination!
@mandar.vaidya
@mandar.vaidya 4 жыл бұрын
thanks nick
@zahash1045
@zahash1045 4 жыл бұрын
could you please zoom out a little further, please? Who wants to see what's being typed on the screen anyways
@akrmh9934
@akrmh9934 4 жыл бұрын
good explanation but the screen is far....i can't see obviously
@buttekaustubhpradeep5566
@buttekaustubhpradeep5566 4 жыл бұрын
Yo nick
@supermadhujya1
@supermadhujya1 4 жыл бұрын
great video man
@demidrek-heyward
@demidrek-heyward 4 жыл бұрын
not sure why people dislike these videos?
@demidrek-heyward
@demidrek-heyward 4 жыл бұрын
like be more appreciative wtf
@karankanojiya7672
@karankanojiya7672 3 жыл бұрын
Beauty Nick !!
@shubhamtiwari6660
@shubhamtiwari6660 4 жыл бұрын
That wasn't a bad Al Pacino impression. And Thanks dude, your videos really inspires me to code.
@yv6358
@yv6358 4 жыл бұрын
you get my stuff. Watch and subscribe so that I can grow my thing. What is he referring to
@son0funiverse
@son0funiverse 3 жыл бұрын
Yeah, this was a doozy
@Vishal-joshi1998
@Vishal-joshi1998 3 жыл бұрын
complex
@jamesgeng7815
@jamesgeng7815 4 жыл бұрын
Man! It does not look like you in this video, you must have drive some vodka...
@ahmedfattah5879
@ahmedfattah5879 4 жыл бұрын
3
@aaronlong1298
@aaronlong1298 2 жыл бұрын
His attitude is kind of the worse, It's extremely hard to get through this because of it. I can feel condescension permeating this video.
@vk1618
@vk1618 4 жыл бұрын
Review
@ajaytyagi3018
@ajaytyagi3018 2 жыл бұрын
f
@HridayGandhi-ix3gl
@HridayGandhi-ix3gl 11 ай бұрын
ur explanations are not clear and understandable you should be first clear urself and then teach others
@abhinavrana5356
@abhinavrana5356 2 жыл бұрын
Pretty stupid explanation tbh
@pankajrathi
@pankajrathi 4 жыл бұрын
Kevin >> Nick
Self Taught Programmers... Listen Up.
11:21
Nick White
Рет қаралды 1 МЛН
هذه الحلوى قد تقتلني 😱🍬
00:22
Cool Tool SHORTS Arabic
Рет қаралды 94 МЛН
LeetCode 124. Binary Tree Maximum Path Sum (Algorithm Explained)
16:16
LeetCode 442. Find All Duplicates in an Array (Solution Explained)
12:37
Top 7 Algorithms for Coding Interviews Explained SIMPLY
21:22
Codebagel
Рет қаралды 373 М.
I gave 127 interviews. Top 5 Algorithms they asked me.
8:36
Sahil & Sarra
Рет қаралды 648 М.
8 patterns to solve 80% Leetcode problems
7:30
Sahil & Sarra
Рет қаралды 348 М.
3 Types of Algorithms Every Programmer Needs to Know
13:12
ForrestKnight
Рет қаралды 469 М.
هذه الحلوى قد تقتلني 😱🍬
00:22
Cool Tool SHORTS Arabic
Рет қаралды 94 МЛН