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

Ғылым және технология

The Best Place To Learn Anything Coding Related - bit.ly/3MFZLIZ
Join my free exclusive community built to empower programmers! - www.skool.com/software-develo...
Preparing For Your Coding Interviews? Use These Resources
--------------------
(My Course) Data Structures & Algorithms for Coding Interviews - thedailybyte.dev/courses/nick
AlgoCademy - algocademy.com/?referral=nick...
Daily Coding Interview Questions - bit.ly/3xw1Sqz
10% Off Of The Best Web Hosting! - hostinger.com/nickwhite
Follow My Twitter - / nicholaswwhite
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/nickwwhite?locale.x...
Become A Member - / @nickwhite
#coding #programming #softwareengineering

Пікірлер: 83

  • @ArjunKalidas
    @ArjunKalidas4 жыл бұрын

    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

    3 жыл бұрын

    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.

  • @zmanneverdie
    @zmanneverdie4 жыл бұрын

    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.

  • @trung.n
    @trung.n3 жыл бұрын

    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

  • @saiyamkumarsingh619
    @saiyamkumarsingh6194 жыл бұрын

    So finally you did this problem. I watched yesterday's live stream when you were solving this.

  • @kittwwang
    @kittwwang3 жыл бұрын

    Your solution is always very concise. I was coding it and got all the index mangled up. Thanks for the video!

  • @suyashsorte
    @suyashsorte4 жыл бұрын

    Great video!!! I took a while to understand the concept. Can you tell what is the time and space complexity for this approach?

  • @AkshayKumar-xh2ob
    @AkshayKumar-xh2ob3 жыл бұрын

    Your solutions always explain the intuition behind the solution.

  • @darod6098
    @darod60984 жыл бұрын

    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)

  • @vinothborn2win
    @vinothborn2win2 жыл бұрын

    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 ....

  • @evgeni-nabokov
    @evgeni-nabokov4 жыл бұрын

    Is there a way to improve look up for the inIndex?

  • @MrThepratik
    @MrThepratik4 жыл бұрын

    nailed it . thanks for the clear explaination

  • @sohamharnale2878
    @sohamharnale28784 жыл бұрын

    Do you solve problems live on Twitch? Would love to be a part of it!

  • @Aditya-rm3bl
    @Aditya-rm3bl3 жыл бұрын

    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

  • @rasikachavan7290
    @rasikachavan72904 жыл бұрын

    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)

  • @anirudhatalmale5575
    @anirudhatalmale55754 жыл бұрын

    Awesome explanation. Thanks

  • @User_Unknown_Anonymous
    @User_Unknown_Anonymous4 жыл бұрын

    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.

  • @lazycoder1910
    @lazycoder19103 жыл бұрын

    Thanks for all ur work!

  • @brovet78
    @brovet784 жыл бұрын

    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?

  • @supermadhujya1
    @supermadhujya14 жыл бұрын

    great video man

  • @cathywei8842
    @cathywei88423 жыл бұрын

    Thank you ! really helpful!

  • @FitnessChaos
    @FitnessChaos3 жыл бұрын

    Good explanation of preorder, inorder and postorder

  • @rodanm
    @rodanm3 жыл бұрын

    Nick, can you share your 2ms submission? Thanks

  • @sophiezhang6516
    @sophiezhang65164 жыл бұрын

    good explaination!

  • @user-bi4gg9he7v
    @user-bi4gg9he7v4 жыл бұрын

    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
    @calfland794 жыл бұрын

    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.

  • @vasanthakumarn7196
    @vasanthakumarn71964 жыл бұрын

    Hii bro can u solve HOUSE ROBBERY 3 problem ? Under tree category

  • @ShubhamVerma-cn7bc
    @ShubhamVerma-cn7bc3 жыл бұрын

    Nice Video, gr8 explaination

  • @mandar.vaidya
    @mandar.vaidya3 жыл бұрын

    thanks nick

  • @nikakondra5321
    @nikakondra532111 ай бұрын

    Thank you Nick!

  • @emansrnme1371
    @emansrnme13714 жыл бұрын

    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 .

  • @karankanojiya7672
    @karankanojiya76723 жыл бұрын

    Beauty Nick !!

  • @user-vu8jp3si2r
    @user-vu8jp3si2r Жыл бұрын

    Why code in this video get stack over flow?

  • @arianphilips5777
    @arianphilips57774 жыл бұрын

    It took me 1 hour to do this problem, and you just did it in 11 minutes DAM

  • @sumandutta83

    @sumandutta83

    3 жыл бұрын

    there was a leetcode pre-existing solution :-)

  • @yashsvidixit7169

    @yashsvidixit7169

    3 жыл бұрын

    And I can't understand it for a month now, FML

  • @Hgh38
    @Hgh383 жыл бұрын

    Anyone think question is hard? I even saw this video and I still don’t get it.

  • @stith_pragya
    @stith_pragya8 ай бұрын

    Thank You So Much for this wonderful video.......🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻

  • @lowkey_anp
    @lowkey_anp4 жыл бұрын

    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

  • @shubhamtiwari6660
    @shubhamtiwari66604 жыл бұрын

    That wasn't a bad Al Pacino impression. And Thanks dude, your videos really inspires me to code.

  • @adamberry7536
    @adamberry75364 жыл бұрын

    You can save the value and index lookup for the inorder in a hashmap to avoid the for loop in your recursive stack.

  • @user-mi8ew2to8e
    @user-mi8ew2to8e4 жыл бұрын

    I didn't know Nelson Bighetti aka 'Big Head' has a youtube channel

  • @killingjoyandmemeing

    @killingjoyandmemeing

    3 жыл бұрын

    lol

  • @buttekaustubhpradeep5566
    @buttekaustubhpradeep55664 жыл бұрын

    Yo nick

  • @trinhta9410
    @trinhta94104 жыл бұрын

    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.

  • @_sf_editz1870
    @_sf_editz18707 ай бұрын

    Coolll easiest explanation on internet

  • @ahmedfattah5879
    @ahmedfattah58794 жыл бұрын

    3

  • @akrmh9934
    @akrmh99344 жыл бұрын

    good explanation but the screen is far....i can't see obviously

  • @zahash1045
    @zahash10454 жыл бұрын

    could you please zoom out a little further, please? Who wants to see what's being typed on the screen anyways

  • @son0funiverse
    @son0funiverse3 жыл бұрын

    Yeah, this was a doozy

  • @vk1618
    @vk16184 жыл бұрын

    Review

  • @Vishal-joshi1998
    @Vishal-joshi19983 жыл бұрын

    complex

  • @yv6358
    @yv63583 жыл бұрын

    you get my stuff. Watch and subscribe so that I can grow my thing. What is he referring to

  • @demidrek-heyward
    @demidrek-heyward3 жыл бұрын

    not sure why people dislike these videos?

  • @demidrek-heyward

    @demidrek-heyward

    3 жыл бұрын

    like be more appreciative wtf

  • @ajaytyagi3018
    @ajaytyagi30182 жыл бұрын

    f

  • @jamesgeng7815
    @jamesgeng78154 жыл бұрын

    Man! It does not look like you in this video, you must have drive some vodka...

  • @aaronlong1298
    @aaronlong12982 жыл бұрын

    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.

  • @pankajrathi
    @pankajrathi4 жыл бұрын

    Kevin >> Nick

  • @HridayGandhi-ix3gl
    @HridayGandhi-ix3gl10 ай бұрын

    ur explanations are not clear and understandable you should be first clear urself and then teach others

  • @abhinavrana5356
    @abhinavrana53562 жыл бұрын

    Pretty stupid explanation tbh

Келесі