LeetCode 235. Lowest Common Ancestor of a Binary Search Tree (Algorithm Explained)

Sdílet
Vložit
  • čas přidán 11. 01. 2020
  • 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
  • Věda a technologie

Komentáře • 43

  • @jaspreets1097
    @jaspreets1097 Před 3 lety +19

    Hey! There is another problem on leetcode which is literally the same exact thing but not a 'binary SEARCH tree', its considered Medium difficulty and I came up with a really inefficient solution haha. Would love to see you explain that since the tree nodes are not sorted. I find your videos really helpful bro, especially this time of year. Keep it up, looking forward to great content!

    • @Turnpost2552
      @Turnpost2552 Před 3 lety

      Be more with specific the names.

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

      I know what you mean, I actually got the two confused initially. It's 236. Lowest Common Ancestor of a Binary Tree. I would like to know how he did that one too.

  • @ziqixu5380
    @ziqixu5380 Před 4 lety +6

    That's awesome! Watching your videos has become my new daily routine.

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

    wow! that's easy! Thank you
    Python:
    class Solution:
    def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
    if p.val < root.val and q.val < root.val:
    return self.lowestCommonAncestor(root.left, p, q)
    if p.val > root.val and q.val > root.val:
    return self.lowestCommonAncestor(root.right, p, q)
    return root

  • @nagarjunareddypadala1585
    @nagarjunareddypadala1585 Před 3 lety +2

    Your explanation and problem solving is great. I would also like to know the time complexity and space complexity with explanation as these will be expected by any interviewer. :)

  • @somyasrivastava6315
    @somyasrivastava6315 Před 2 lety

    This is so beautiful Nick..Thanks for the explanation.

  • @rishabhjain4546
    @rishabhjain4546 Před 3 lety +2

    You treat problems as if they are nothing. I like that attitude so much. Makes any problem easier.

  • @pealhasan
    @pealhasan Před 6 měsíci

    bro everytime you explain a solution it sounds so easy 😭GOAT

  • @1levente371
    @1levente371 Před rokem +1

    i think there is a problem, if p and q is 0,and 5 in the first example then it would just return the root?which is not correct

  • @anmolbindroo7354
    @anmolbindroo7354 Před 4 lety

    What will happen in the case of two nodes directly connected to each other?

    • @guambomber448
      @guambomber448 Před 2 lety

      It will return the parent of the two nodes connected to each other because both if statements will return false

  • @dishagupta7446
    @dishagupta7446 Před 3 lety

    What will ne time complexity

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

    Love the explanations!

  • @dunkyzhang2067
    @dunkyzhang2067 Před 4 lety +6

    runtime error: member access within null pointer of type 'TreeNode'

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

      yeah i was waiting on him to click on submit..

    • @shraddhashetty8659
      @shraddhashetty8659 Před 3 lety +2

      I got the same error but that was because i was missing return keyword within the if condition.
      if(p.valroot.val) {
      *return* lowestCommonAncestor(root.right,p,q);
      }
      works fine for me

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

      @@shraddhashetty8659 interesting i still see that error even with the returns.

    • @AnandBaburajan
      @AnandBaburajan Před 3 lety +3

      Maybe you are looking for the solution to 'binary tree' and not 'binary search tree'

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

      @@AnandBaburajan Thanks man! I was new to coding and literally executed this same code (binary search tree) in binary tree question on leetcode. 😄

  • @dimas-
    @dimas- Před 4 lety

    This is brilliant

  • @shivanshnegi6957
    @shivanshnegi6957 Před rokem

    keep up the good work brother

  • @chrisyang05
    @chrisyang05 Před rokem +1

    This solution assumes the nodes being searched exist somewhere in the tree i.e. no invalid input.

  • @AbhishekSharma-kc7ic
    @AbhishekSharma-kc7ic Před 3 lety +1

    where is base case if root is null and why its not there?

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

      no base case required as he is returning the root anyway.

  • @GauravSingh-ku5xy
    @GauravSingh-ku5xy Před 2 lety

    Thanks buddy.

  • @sharmadenesh
    @sharmadenesh Před 3 lety

    This will throw null pointer exception if root itself is null

  • @harenderbhardwaj6357
    @harenderbhardwaj6357 Před rokem +1

    some test cases are failing on leetcode for this solution

    • @1levente371
      @1levente371 Před rokem

      yes if p and q is 0 and 5 in the first example and it would be wrong

  • @JohnWick-kh7ow
    @JohnWick-kh7ow Před 2 lety

    It's giving me runtime error.

  • @ScaerCroe
    @ScaerCroe Před 3 lety

    Nice shirt!

  • @user-df8eq7hu9b
    @user-df8eq7hu9b Před 4 lety

    good

  • @marcoflores1764
    @marcoflores1764 Před 4 lety

    Nice

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

    Man damn you are a super genius 👌

  • @ahmedfattah5879
    @ahmedfattah5879 Před 4 lety

    6

  • @prashantsah5901
    @prashantsah5901 Před 3 měsíci

    This solution only works for the Binary Search Tree (BST) and not for the General Binary Tree.

  • @Vishal-joshi1998
    @Vishal-joshi1998 Před 3 lety

    wow

  • @AniketSomwanshi-ll7mz
    @AniketSomwanshi-ll7mz Před 3 lety

    Correction in the code :
    if(p->val == root->val || q->val == root->val)
    return root;
    This will be the first thing to check. Thanks for the solution @Nick White

    • @dhanushkumar9639
      @dhanushkumar9639 Před 3 lety

      error -->runtime error: member access within null pointer of type 'TreeNode' (solution.cpp)

    • @AniketSomwanshi-ll7mz
      @AniketSomwanshi-ll7mz Před 3 lety

      @@dhanushkumar9639 check if root itself is null

  • @Viz4rd
    @Viz4rd Před 8 měsíci

    I just feel dumb now:)