Lowest Common Ancestor of a binary tree | Leetcode

Sdílet
Vložit
  • čas přidán 25. 09. 2021
  • This video explains a very frequently asked programming interview problem which is to find the lowest common ancestor in the given binary tree.I have first explained the problem statement followed by concepts of lowest common ancestor using examples and then finally showed 2 different ways to solve the problem. I have shown the first solution by finding the last common node in the root to node path. The second solution is based on simple recursion.
    CODE LINK is present below as usual. If you find any difficulty or have any query then do COMMENT below. PLEASE help our channel by SUBSCRIBING and LIKE our video if you found it helpful...CYA :)
    ======================================PLEASE DONATE=============================
    🧡 SUPPORT OUR WORK: / techdose
    💚 UPI-ID: surya.kahar@ybl
    💞JOIN Membership: / @techdose4u
    ==============================================================================
    INSTAGRAM : / surya.pratap.k
    LinkedIn: / surya-pratap-kahar-47b...
    WEBSITE: techdose.co.in/
    TELEGRAM Channel LINK: t.me/codewithTECHDOSE
    TELEGRAM Group LINK: t.me/joinchat/SRVOIxWR4sRIVv5...
    =======================================================================
    USEFUL LINKS:
    🟠Must do TIPS to ACE Virtual Interview: • 🔴Must do Tips to ACE y...
    🟢Best strategy to excel your coding interview: • 🔴Best strategy to exce...
    🟡Get your dream job in 1 month: • 🔴Get your dream job in...
    🔵How to crack dream job in just 2 months: • How to crack dream job...
    🟣7 Days DSA plan: techdose.co.in/7-days-dsa-che...
    RELATED LINKS:
    Vertical Order Traversal of Binary Tree: • Vertical order travers...
    Unique BSTs: • Unique Binary Search T...
    Count Complete Tree Nodes: • Count Complete Tree No...
    CODE LINK: gist.github.com/SuryaPratapK/...

Komentáře • 48

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

    You're an excellent teacher, I've learned so much from your videos, you explain recursion beautifully, Thank you!

  • @praveengautam4689
    @praveengautam4689 Před rokem

    you explaied node tree recursion like a class 8th maths problem. Bro you're a genius.

  • @mangoman2023
    @mangoman2023 Před 2 lety

    Very nice explanation. Thank you

  • @sirichandanayadav5249
    @sirichandanayadav5249 Před rokem +1

    Happy teacher's day Sir...

  • @amratef3245
    @amratef3245 Před 11 měsíci +1

    I don't comment very often on videos, but yours really helped me. Thank you so much

  • @albindavis3538
    @albindavis3538 Před rokem

    great explanation theyw ay you explain the question by all possibe senario is superb

  • @ritikmishra5210
    @ritikmishra5210 Před 2 lety +2

    Man, you're awesome as always!

  • @nitinsaxena1857
    @nitinsaxena1857 Před 2 lety

    Excellent.

  • @areebashaikh22
    @areebashaikh22 Před 5 měsíci

    Thank you!

  • @DaudZaidi
    @DaudZaidi Před 2 lety +6

    Nice one. Please do Leetcode 621 - Task Scheduler.

  • @sharonwang592
    @sharonwang592 Před rokem

    Very smart! Thanks

  • @devbhattacharya153
    @devbhattacharya153 Před rokem

    Thanks a lot sir :)

  • @santoshrathod9868
    @santoshrathod9868 Před 2 lety

    Thanks brother

  • @harpercfc_
    @harpercfc_ Před rokem

    THIS IS AWESOME.

  • @chrisliewjy
    @chrisliewjy Před 2 lety +2

    Great stuff! 👍🏻

  • @charkhude
    @charkhude Před 2 lety +4

    Thanks

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

    Nicely explained !!

  • @anupam6045
    @anupam6045 Před rokem

    Best expalnation ever

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

    Great explanation.

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

    Awesome!!

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

    Very much excited

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

    TECH DOSE, do you plan to add playlists according to the Leetcode's difficulty level? That would increase views for you and help the audience to watch the whole list of problems that they target. Thanks.

  • @ui.ashish
    @ui.ashish Před 2 lety +2

    amazing content

  • @biswajitmahato4413
    @biswajitmahato4413 Před 2 lety

    What is the editor name?

  • @sunny-14689
    @sunny-14689 Před 2 lety +1

    Can you also discuss on side how to solve if links to parent is present (i.e each node contains link to parent) ?

  • @shrutibansal5261
    @shrutibansal5261 Před rokem +1

    👏👏👏

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

    Your final commentary is a lot confusing and doesn’t explain why you return left or right when the other is null. It is because when right is null you found both modes on left so the left node down is LCA and vice-versa

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

      Take some examples and it will be clear for sure

  • @maruthkamath9304
    @maruthkamath9304 Před 2 měsíci

    ~12.30 is wrong.LCA could have been 5 also . So the conclusion is wrong. I guess needs an edit.

  • @JangBahadur3028
    @JangBahadur3028 Před 2 lety +2

    Wait is over. 😊

  • @andrepinto7895
    @andrepinto7895 Před 2 lety

    This makes unnecessary work. Imagine that both nodes are on the left, your code still explores the entire right subtree, when it could already stop the search there. You can keep state locally in the class or use an helper method with an additional settable field to avoid this. On the other hand, the fact that you don't continue the search down the subtrees when root matches one of the nodes, reduces the search space, so depending on the case, it might end up being better or worse.

  • @calvio2835
    @calvio2835 Před 9 měsíci

    such an awful explanation