Lowest Common Ancestor Binary Tree

Sdílet
Vložit
  • čas přidán 26. 04. 2016
  • / tusharroy25
    github.com/mission-peace/inte...
    github.com/mission-peace/inte...
    Given two nodes find lowest common ancestor of these 2 nodes in the binary tree

Komentáře • 334

  • @cheofusi3562
    @cheofusi3562 Před 4 lety +66

    Recursion is indeed a divine art

    • @blatogh1277
      @blatogh1277 Před 3 lety

      It is easy to go through, and everything is so nice, besides if the binary tree contains Integer.MAX_VALUE layer, for example, the call stack may be passing away.

    • @kylanali9575
      @kylanali9575 Před 2 lety

      you probably dont give a damn but does any of you know of a way to log back into an Instagram account??
      I was stupid lost the login password. I would love any tips you can give me

    • @coltonjad4825
      @coltonjad4825 Před 2 lety

      @Kylan Ali instablaster =)

  • @ok123ut
    @ok123ut Před 3 lety +29

    the way this was explained in 11 min. I don't think i can find more valuable solutions for this in such a short time anywhere else. Amazing!

  • @kevinbaijnath6974
    @kevinbaijnath6974 Před 7 lety +16

    Wow, great explanation about the algorithm! I appreciated the fact that you gave multiple examples that tested different things!

  • @cachegrk
    @cachegrk Před 8 lety +2

    You are videos are just awesome! Great job! Can't imagine prep without your videos!

  • @dianel.344
    @dianel.344 Před 8 lety +13

    Thank you, sir! I was confused about this recursive code, but now I understand how it works perfectly!

  • @dnl_blkv
    @dnl_blkv Před 7 lety +16

    Another great explanation with a beautiful solution!

  • @leonlew1386
    @leonlew1386 Před 7 lety +2

    This was a great visual walkthrough. Clear and concise.

  • @depression_plusplus6120
    @depression_plusplus6120 Před 3 lety +25

    After 5 years he's still helping

  • @shreekantwaphare6063
    @shreekantwaphare6063 Před 7 lety +9

    The space complexity of both the algorithms is O(height), because the recursive algorithm will have function calls on the stack.

  • @xuwang3253
    @xuwang3253 Před 5 lety +11

    I love this guy's multiple walking-through examples on white board!

  • @cosmicgirl910
    @cosmicgirl910 Před 4 lety +1

    Thank you for covering multiple cases. Other videos didn’t explain as thoroughly as you and didn’t go through many test cases

  • @antuancaraballo9691
    @antuancaraballo9691 Před 8 lety +3

    Love your explanations Tushar! You are awesome!

  • @abdallaelmedani8933
    @abdallaelmedani8933 Před 3 lety +1

    This is a very useful, well explained, and simple algorithm. Thank you, Tushar.

  • @anandsakthivel9425
    @anandsakthivel9425 Před 6 lety

    Thank you so much for your contribution..I think it is one of the best lectures i have ever seen for like this focusing on interview problems alone..Again thank you so much...please keep on share more and more challenging problems

  • @decisionguider4911
    @decisionguider4911 Před 4 lety +1

    Great job Tushar! Absolutely awesome walkthrough..

  • @jinny5025
    @jinny5025 Před 3 lety +1

    Your explanation always hits the spot. Thanks :)

  • @generalroboskel
    @generalroboskel Před 7 lety +1

    Excellent video/explanation. Your videos make algorithms so much easier.

  • @mtjokro
    @mtjokro Před 3 lety +5

    Thank you! Finally a video that helped me understand the recursive details of the various cases of binary tree

  • @xxbighotshotxx
    @xxbighotshotxx Před 6 lety +3

    Thank you for the explanation! I was able to write the code for it myself as a result!

  • @14rucha
    @14rucha Před 7 lety +1

    Awesome video! Your explanation is too good. I can understand recursion very easily through your video.

  • @shubham709
    @shubham709 Před 5 lety +1

    Great job, the explanation is very clear and lucid. Thanks a lot :).

  • @jlecampana
    @jlecampana Před 3 lety +1

    Best Video on LCA available on CZcams, Thanks Tushar!

  • @guru007pavan
    @guru007pavan Před 4 lety +13

    Too good explanation. short and sweet. I can say this particular solution is way better explained than Back ToBack SWE. Thanks and appreciate your effort...

  • @jaspertienny8976
    @jaspertienny8976 Před 7 lety +1

    The amazing Tushar. Thanks!

  • @nikkis8102
    @nikkis8102 Před 7 lety +1

    Thank you so much, Tushar!!

  • @madiyar8079
    @madiyar8079 Před 4 lety

    amazing!! Thanks Tushar! PLease continue!

  • @sharansingh4956
    @sharansingh4956 Před 3 lety +1

    After i get tired of the logics and code explained by everyone, i do watch your video to understand it in layman and practical way.

  • @wendaoliu9447
    @wendaoliu9447 Před 6 lety

    I really enjoy your video, it's so clear and helpful, many thanks!!!!!

  • @shreyasingh-sn4bs
    @shreyasingh-sn4bs Před 7 lety +2

    I have become a fan! Great work Sir :)

  • @manishbhatt3614
    @manishbhatt3614 Před 8 lety

    @Tushar: Good work and good explanations.

  • @nimishbajaj3815
    @nimishbajaj3815 Před 8 lety +2

    Great work, as you recommended I started solving problems on GeeksForGeeks, your videos + that website together make up a great learning experience thanks again, keep up the good work !

  • @arpanjain3560
    @arpanjain3560 Před 5 lety +6

    Really helpful. Thanks a lot. And hoping to cover lots of more DS interview questions in future videos. 👍👍 And special thanks for the code. Bcoz most of got stuck into code part even after knowing the logic.

  • @sleepypanda7172
    @sleepypanda7172 Před 3 lety

    great explanation! this dry run really helped me a lot

  • @iamyaqoob
    @iamyaqoob Před 8 lety +2

    Wow! Great explanation!

  • @hmoiz52
    @hmoiz52 Před 6 lety +1

    Very nicely explained! Thanks.

  • @robinsoni4778
    @robinsoni4778 Před 3 lety +15

    The other solution also takes the implicit stack during recursion, so yes it does require the extra space.

  • @vuleenguyen
    @vuleenguyen Před 5 lety +2

    Thanks a lot. You gave me the idea to solve this problem

  • @jubiliudu
    @jubiliudu Před 2 lety

    Your solutions are amazing!! Keep doing it :)

  • @rohitkumar-yz5qy
    @rohitkumar-yz5qy Před 6 lety +1

    you have given best explanation..thanks a lot

  • @sumedhaj9017
    @sumedhaj9017 Před rokem

    Very nice explanation with the examples!

  • @HieuLe-mh4bc
    @HieuLe-mh4bc Před 2 lety

    Great explanation! Thank you for the video.

  • @rajusaratkar2599
    @rajusaratkar2599 Před 8 lety +2

    Very nice and Clear Explanation ..Thanks for the Video.

  • @levtunik997
    @levtunik997 Před 4 lety +1

    great explanation, you have high-quality videos..thanks.

  • @kfliden
    @kfliden Před 3 lety +1

    Great explanation as usual!

  • @nurture_your_hobby
    @nurture_your_hobby Před 8 lety +1

    Nice examples to illustrate...thanks a lot buddy :-)

  • @sureshgarine
    @sureshgarine Před 2 lety

    hi Thushar, so nice of you...explained very well

  • @lpatrasco
    @lpatrasco Před 5 lety

    Clear and straight to the point.

  • @joycer5293
    @joycer5293 Před 3 lety +1

    thank you so much this is the most helpful video ive found for this question

  • @gourab469
    @gourab469 Před 2 lety

    Excellent explanation! Hats off!

  • @evantheking
    @evantheking Před 6 lety +1

    finally good explanation. I understand it now

  • @sase1017
    @sase1017 Před 3 lety +1

    Excellence! Real knowledge, thank you.

  • @shubhamkale735
    @shubhamkale735 Před 2 lety

    Thank you sir for this dry run feeling lucky to find this video ... Keep making such dry run video sir Thank you

  • @SiddharthKulkarniN
    @SiddharthKulkarniN Před 6 lety +1

    Great videos dude. Thanks!

  • @learnsharegrow7294
    @learnsharegrow7294 Před 2 lety

    Nicely explained. This is a popular problem statement in Amazon interviews.

  • @nadiiaparsons6088
    @nadiiaparsons6088 Před 2 lety

    It's much clear now. Thanks a lot for your explanation.

  • @444not
    @444not Před 3 lety

    Thank you for the explanation. Very helpful.

  • @bumbarumbado
    @bumbarumbado Před 8 lety +1

    beautiful explanation!

  • @vaibhavlodha5398
    @vaibhavlodha5398 Před 5 lety

    Thank you very much. this is great!

  • @swapnilpatel6582
    @swapnilpatel6582 Před 8 lety +1

    Salute to u Man !! _/\_ U are a blessing :)

  • @sase1017
    @sase1017 Před 3 lety +1

    Thanks for the great explanation

  • @saptarshi9433
    @saptarshi9433 Před 5 lety

    Thank you . Good one . You are awesome. from 10.55 the way you talked, looks like u are iterating an array ['like', 'share', 'subscibe'] + 'this video'

  • @reassume4826
    @reassume4826 Před 2 lety

    above & beyond awesomeness!

  • @shubhamtalks9718
    @shubhamtalks9718 Před 4 lety +1

    Amazing explanation!!!

  • @foreverursabhi
    @foreverursabhi Před 3 lety +4

    Optimization note, if the left subtree search yielded the LCA, there's no need to search the right subtree. This can only be avoided by keeping track of how many of the target nodes have been found; it's not sufficient to check if the left subtree search returned a node that is not one of the target nodes, because one of the target nodes may as well be the LCA. For example, 6, and 2.

    • @dileepbc5901
      @dileepbc5901 Před 2 lety +1

      ASSUMTION:
      We may assume that either both n1 and n2 are present in the tree or none of them are present.

    • @dileepbc5901
      @dileepbc5901 Před 2 lety +1

      NOTE : optimisation not works for special case

  • @shreyaa_21
    @shreyaa_21 Před 3 lety

    Recursion Explained nicely, thanks

  • @chrisy.703
    @chrisy.703 Před 2 lety

    best lca video in youtube so far!

  • @stonecoldcold2941
    @stonecoldcold2941 Před 4 lety +1

    Awsome Explanation Bro!!

  • @rajatsharma_02
    @rajatsharma_02 Před 8 lety +1

    nice, cool stuff u r doing !

  • @chetansarnad1232
    @chetansarnad1232 Před 3 lety

    Thanks for the video, really helpful

  • @11m0
    @11m0 Před 8 lety +1

    Great explanation...thanks

  • @sait5489
    @sait5489 Před 8 lety +1

    Thank u very much sir ... for ur explanation

  • @adithiaa4385
    @adithiaa4385 Před 3 lety

    Great explanation! Thank you.!

  • @mohammedghabyen721
    @mohammedghabyen721 Před 3 lety

    Great explanation, Thanks a lot

  • @bipul2138
    @bipul2138 Před 4 lety

    Genius approach...well explained

  • @ronakgupta7492
    @ronakgupta7492 Před 8 lety +1

    Hi Tushar , Your videos has helped me alot . Thanks for the wonderful series . Sometimes i face problems while running through the call stack in recursion problem , i mean i find it difficult to get the flow . Could you pls make a video for that !!Thanks :)

  • @DanielLee-et4sk
    @DanielLee-et4sk Před 4 lety

    Awesome Explanation!

  • @AP-eh6gr
    @AP-eh6gr Před 8 lety +103

    in the last 8,7 example, is there an assumption that both given nodes whose ancestor we are looking for, have to be present in the tree?
    bcoz we returned 8, and never went down to 7 to see if it was actually there or not

    • @hemanthp2367
      @hemanthp2367 Před 7 lety +5

      I think we should make assumptions clear that is both nodes should exist and nodes are not equal, tree with one node etc?
      static Node lca(Node rootNode, int a, int b, AtomicBoolean found) {
      if (rootNode == null) {
      return null;
      }
      if (found.get()) {
      return null;
      }
      Node left = lca(rootNode.left, a, b, found);
      Node right = lca(rootNode.right, a, b, found);
      if (left != null && right != null) {
      found.set(true);
      return rootNode;
      }
      final boolean rootMatched = rootNode.i == a || rootNode.i == b;
      if (rootMatched) {
      if (left != null || right != null || a == b) {
      found.set(rootMatched);
      }
      return rootNode;
      }
      if (left != null) {
      return left;
      } else if (right != null) {
      return right;
      } else {
      return null;
      }
      }
      This should work for all the cases. I used AtomicBoolean but can be replaced by MutableBoolean.
      You have a match only when found returns true.

    • @mrdude1084
      @mrdude1084 Před 5 lety +1

      I saw the solution to this problem over gfg but couldn't get the part of searching even after we were using maps for given 2 values... thnx for your question.. helped me alot

    • @nishantharava9181
      @nishantharava9181 Před 5 lety +3

      Even if we traverse until node 7, it would eventually return the same value 8 to the root of the tree. This case holds true every time when one of the nodes is a ancestor.

    • @abhinavmishra7617
      @abhinavmishra7617 Před 4 lety +4

      this algo is based ln the assumption that that both nodes will be present in the tree...nice observation btw

    • @abhinavmishra7617
      @abhinavmishra7617 Před 4 lety +3

      4 years ago....damn

  • @AlancRodriguez
    @AlancRodriguez Před rokem

    Six years later and im still learning from you

  • @b9944236
    @b9944236 Před 11 měsíci

    Last example is really important to explain both nodes in the same path.

  • @udaysaiphanindra3138
    @udaysaiphanindra3138 Před 4 lety

    Nice explanation, thank you sir.

  • @ayushpalak
    @ayushpalak Před 8 lety +1

    great video Tushar. Thanks for taking out your time to explain all these stuffs. They really help a lot. Btw, do you speak Hindi ? :P

  • @adampatrick7687
    @adampatrick7687 Před 4 lety +1

    Really really helpful.

  • @ShaliniNegi24
    @ShaliniNegi24 Před 4 lety

    Very nice Explanation.

  • @user-um8bh2tq8p
    @user-um8bh2tq8p Před 5 lety

    Tushar is the best!

  • @vbnandu867
    @vbnandu867 Před 8 lety +1

    just wondering, if i could possibly come up with this optimized soln in an actual interview...

  • @sohiebmohamed481
    @sohiebmohamed481 Před 8 lety +10

    Could you please explain also the Lowest Common Ancestor in general trees using sparse table or RMQ :)

  • @haoli2976
    @haoli2976 Před 2 lety

    Genius. Thank you very much.

  • @biboswanroy6699
    @biboswanroy6699 Před 4 lety +7

    In the recursive solution isn't anyway space is consumed due to stacks

    • @MIRAZIB
      @MIRAZIB Před 4 lety

      yes, obviously .. space complexity is O( h ) .. worst case will occur when the tree is skewed and the ancestor lies beneath of it.

  • @HoanNguyen-fc8vb
    @HoanNguyen-fc8vb Před 2 lety

    Very nice. Thank you very much.

  • @saravanprathi6956
    @saravanprathi6956 Před 4 lety

    God bless you man!

  • @RottenWoodInPower
    @RottenWoodInPower Před 4 lety

    Very clear. Thanks

  • @0xeb-
    @0xeb- Před 3 lety

    Perfect. Thank you.

  • @shimul6680
    @shimul6680 Před 2 lety

    Thank you very much.....! Super solution!!!

  • @rhondawang2639
    @rhondawang2639 Před 3 lety +1

    Thank you so much!

  • @xiaolong9446
    @xiaolong9446 Před 3 lety

    Helpful! Thanks.

  • @umberto3271
    @umberto3271 Před 2 lety

    this is excellent, thank you

  • @raman-jn6yt
    @raman-jn6yt Před 4 lety

    nicely explained !!!

  • @BhatiJhabarSingh
    @BhatiJhabarSingh Před 3 lety

    Excellent Explaination

  • @beginner1271
    @beginner1271 Před 8 lety

    simplest explanation i have ever seen.. awesome _/\_

  • @mayankmittal6552
    @mayankmittal6552 Před 4 lety +7

    man, your hair style and explanation a true entertainer!!

    • @uRamPlus
      @uRamPlus Před 3 lety

      Hahahaha !!! He’s cool tho