Lowest Common Ancestor Binary Search Tree

Поделиться
HTML-код
  • Опубликовано: 29 сен 2024
  • / tusharroy25
    github.com/mis...
    github.com/mis...
    Find lowest common ancestor in binary search tree.

Комментарии • 75

  • @_PRITIJHA
    @_PRITIJHA 3 года назад

    Your Explanation is awesome

  • @TusharMudgal
    @TusharMudgal 8 лет назад +14

    Hey tushar, can you please upload video about The Great Tree-List Recursion problem. I have been following your tree lectures and it is as always well explained except it misses one of the most asked interview problem.

    • @ruitu6005
      @ruitu6005 8 лет назад

      this has my vote as well

    • @arvindhmani06
      @arvindhmani06 7 лет назад +8

      If Tushar can't convince Tushar then nobody can

  • @itsnpe
    @itsnpe 8 лет назад +6

    Hii, You have a very cool way of teaching advanced data structures and algorithms, why don't you now have a go a competitive coding , it will be a huge boom to peeps.

  • @prekshakoirala6331
    @prekshakoirala6331 7 лет назад +8

    Damn. The best method ever.

  • @ruitu6005
    @ruitu6005 8 лет назад +2

    Awesome video, this solution is even more neat than the solution presented in "The Element of Programing Interview". Thanks!

  • @eugeniaqing
    @eugeniaqing 7 лет назад +2

    Thanks for your video, it's very helpful.

  • @kushaladhvaryu
    @kushaladhvaryu 7 лет назад +1

    if(root.data > Math.max(n1.data, n2.data)){
    return lowestCommonAncestor(root.left,n1,n2);
    }else if (root.data < Math.min(n1.data,n2.data)){
    return lowestCommonAncestor(root.right,n1,n2);
    }else{
    return root;
    }
    Here in conditions we will compare with root.data not just root as one is value other is node.

    • @angshumansarma2836
      @angshumansarma2836 5 лет назад

      yup that correction should be there,but is quite mindful anyways...

  • @algorithmicthoughts8565
    @algorithmicthoughts8565 8 лет назад +1

    Thanks for this video very helpful. Can you please have a video explaining the O(n) time suffix array construction using Karkaainen. Thanks in advance.

  • @fabricator.cap.hill.seattle
    @fabricator.cap.hill.seattle 2 года назад

    0:44 "looking for diverged in different paths and if one node is an ancestor of the..." But how the heck am I supposed to figure that out quickly in a coding interview?

  • @deepakbammi3967
    @deepakbammi3967 7 лет назад +2

    It should be root.data instead of root. :)

  • @harishdoddi322
    @harishdoddi322 5 лет назад +1

    Awesome Tushar. Great videos!

  • @deepanshugarg4713
    @deepanshugarg4713 8 лет назад +1

    Hey Tushar, can you please make a video on treaps.
    Thanks

  • @niachungdupit8104
    @niachungdupit8104 3 года назад

    Since all the give example are either greater or lesser than root node 10 so we know where to move..but what if we had ,-10 and 30 then how we move

  • @kaushikjp
    @kaushikjp 8 лет назад +1

    Hi Tushar, You have condition like if (root > max (n1.date,n2.data)) , which max will return integer value and you are comparing with Node which is pointer so it will not compile code

    • @jahirulislammonir3490
      @jahirulislammonir3490 8 лет назад +1

      In here may be if(root->data >(n1->data && n2->data)) will be in code.. But it's just for conception...

    • @kaushikjp
      @kaushikjp 8 лет назад

      Ya i got it. Thanks

  • @MrityunjayRanjan
    @MrityunjayRanjan 5 лет назад

    Small correction at first line-- > if(root.data > max(n1.data,n2.data))

  • @RafiqulIslam-je4zy
    @RafiqulIslam-je4zy 3 года назад

    Excellent. Thanks a lot.

  • @abhishekshaw9861
    @abhishekshaw9861 2 года назад

    Made it really lucid , Sir😎

  • @SC-uv5le
    @SC-uv5le 3 года назад

    Cool and simple solution!

  • @ivsaimanoj
    @ivsaimanoj 8 лет назад

    Hi Tushar, nice explanation! One question - What if we got to check the existence of n1 and n2 in this tree? Consider they are not present, so this algo will return me an output. For ex: Node "-20" and Node "7" will return me Node "-10"..

  • @kautukraj
    @kautukraj 4 года назад +1

    Very helpful!

  • @debajitsaikia1
    @debajitsaikia1 8 лет назад

    Hey Tushar, Excellent explanation.
    Please upload some linked list related problems as well.

  • @priyadarshanr296
    @priyadarshanr296 3 года назад

    Why isn't the lowest common ancestor of (6,9) is -10

    • @nikhilkv5564
      @nikhilkv5564 Год назад

      -10 is also common ancestor of 6 and 9, but it’s not the lowest. 8 is the parent of of 6 and 9. Hence it would be the LCA.

  • @ernestina475
    @ernestina475 7 лет назад

    Can you please do a lesson on finding LCA of Bst without the parent pointer reference?Thank you

  • @adarshsharmanit356
    @adarshsharmanit356 4 года назад

    MNNITIANS like Here

  • @诸葛俊伟
    @诸葛俊伟 8 лет назад

    Tushar, Can you make a video of "Word Square" question in LeetCode, which I think is one of the most difficult question in LeetCode.

  • @judemartin8369
    @judemartin8369 8 лет назад +1

    Fantastic as always !

  • @MrThepratik
    @MrThepratik 4 года назад

    precise solution

  • @chiquiro10
    @chiquiro10 2 года назад

    eres un papu we

  • @davidenunin886
    @davidenunin886 4 года назад

    Thanks it helped me a lot with the italian computer science olimpics

  • @emersonmicu1683
    @emersonmicu1683 8 лет назад

    Thanks alot, it's very easy to understand it.

  • @Simply_maniyaa
    @Simply_maniyaa 8 лет назад

    Superb Solution. I tried modifying the solution for "Lowest Common Ancestor in Binary tree", but this ones even better

  • @pragyamukherjee8243
    @pragyamukherjee8243 2 года назад

    Thanks !

  • @deepak-ui5li
    @deepak-ui5li 6 лет назад

    Thank You Tushar Roy you are an absolute beast

  • @akashreddy7725
    @akashreddy7725 2 года назад

    Thanks

  • @Miss_iryna_
    @Miss_iryna_ 8 лет назад

    Thanks Tushar! I like your explanation. Please make a video about quicksort.

    • @prekshakoirala6331
      @prekshakoirala6331 7 лет назад

      mycodeschool has a very good video on quicksort if you still need it.

  • @manofacertainrage856
    @manofacertainrage856 7 лет назад

    Thank you for making these videos.

  • @AshutoshKumar-il3wk
    @AshutoshKumar-il3wk 3 года назад

    thanks explained so well !!!

  • @rashmithkoundinya
    @rashmithkoundinya 7 лет назад

    Hey tushar the complexity for the solution is log N right where N is no. of nodes

    • @vitaly4097
      @vitaly4097 7 лет назад

      Nope, this is O(N) solution as tree can be unbalanced, check out www.topcoder.com/community/data-science/data-science-tutorials/range-minimum-query-and-lowest-common-ancestor for log solution.

  • @ashusingh4937
    @ashusingh4937 7 лет назад

    I think the return value of function should be Node *, not Node. We are returning pointer to a node, not actual node.

    • @egor.okhterov
      @egor.okhterov 7 лет назад

      ashu singh In Java 'Node' is a reference and he writes the code in Java.

  • @dragoxofield
    @dragoxofield 8 лет назад

    Best explanation I have seen!

  • @ivaylopankov7369
    @ivaylopankov7369 6 лет назад

    This solution assumes that both n1 and n2 are present in the tree, right? Maybe its worth mentioning.

    • @codeonmars579
      @codeonmars579 5 лет назад

      If n1 and n2 both are not present then still solution is valid. In that case nothing will be returned.

  • @siddharthsethia5986
    @siddharthsethia5986 6 лет назад

    Keep doing the good work sir!!

  • @TheCowsrkewl
    @TheCowsrkewl 8 лет назад

    This is really helpful!

  • @MoharnabSaikia
    @MoharnabSaikia 8 лет назад

    Good job.Love ur videos :)

  • @sivakishorejaladi3482
    @sivakishorejaladi3482 7 лет назад

    Awesome, simple super

  • @arunvyas27
    @arunvyas27 8 лет назад

    Hello Tushar, Very simple and elegant Algo, Thanks. Just curious, If i give input as 30,79: 79 not being in tree, I will get 30. Is this a valid answer?

    • @arunvyas27
      @arunvyas27 8 лет назад

      +arun vyas Also need to do null check, or items not present in tree.

    • @arunvyas27
      @arunvyas27 8 лет назад

      +Tushar Roy Thank you Tushar.

  • @saptarshimitra1267
    @saptarshimitra1267 7 лет назад

    Nice and crisp. TY

  • @jimmy1681000
    @jimmy1681000 6 лет назад

    Really appreciate!!!!!!!

  • @abhisekhagarwala9501
    @abhisekhagarwala9501 5 лет назад

    how does this code handles the last case i.e 30 and 78

    • @meikelk882
      @meikelk882 5 лет назад

      It returns the root because it doesn't pass the condition at the node 30; (30)root < min(30, 78) since the minimum 30 isn't shorter but equal to 30.

  • @mcw0805
    @mcw0805 7 лет назад

    This doesn't consider the case where one or both of the elements doesn't exist in the tree. In case of the former, it will return the one that is present in the tree and not consider the other data (that DNE) at all.

    • @maaz3587
      @maaz3587 7 лет назад +1

      If the elements don't exist then how can their ancestor exist?

    • @egor.okhterov
      @egor.okhterov 7 лет назад

      You check that condition before calling this function.

  • @SaurabhSingh-db8xq
    @SaurabhSingh-db8xq 6 лет назад

    Thanks bhaiya!

  • @Bakepichai
    @Bakepichai 7 лет назад

    Short & sweet

  • @nishithakur424
    @nishithakur424 7 лет назад

    gr8

  • @MountCook
    @MountCook 7 лет назад

    Beautiful!

  • @TejasPatil-lu6zf
    @TejasPatil-lu6zf 7 лет назад

    cool

  • @abhisheksharma3553
    @abhisheksharma3553 7 лет назад +1

    i THINK THERE IS A MISTAKE IN THE CODE.....It shouldnt be the max of both node's data... the root node should be less than both of the n1 and n2.......
    Node lca(Node node, int n1, int n2)
    {
    if (node == null)
    return null;
    // If both n1 and n2 are smaller than root, then LCA lies in left
    if (node.data > n1 && node.data > n2)
    return lca(node.left, n1, n2);
    // If both n1 and n2 are greater than root, then LCA lies in right
    if (node.data < n1 && node.data < n2)
    return lca(node.right, n1, n2);
    return node;
    }
    this is correct....

  • @goloth
    @goloth 6 лет назад +1

    10 is a common ancestor to 30 and 78, so the lowest common ancestor should be 10, not 30.