Unique Binary Search Trees | Count all structurally unique BSTs | Catalan number | Leetcode #96

Sdílet
Vložit
  • čas přidán 23. 06. 2020
  • This video explains a very important programming interview problem which is to count the number of structurally unique binary search trees (BSTs) having N nodes.I have shown the idea of conversion of this problem from given state to finding the Nth catalan number.This problem can be solved by recursion, dynamic programming and also simply by using the binomial formula for finding Nth catalan number.I have taken sufficient examples to show the intuition and approach and at the end of the video, i have also shown the code walk-through. 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 :)
    =================================================================
    INSTAGRAM: / surya.pratap.k
    LinkedIn: / surya-pratap-kahar-47b...
    WEBSITE: techdose.co.in/
    =================================================================
    CODE LINK: gist.github.com/SuryaPratapK/...
    CATALAN NUMBER: • How to calculate Catal...

Komentáře • 72

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

    Most intuitive, thanks a ton buddy, I was reading Leetcode blogs for 3 days with all crappy methods, yours is simply brilliant and clear !

  • @bhuwansingh2534
    @bhuwansingh2534 Před 2 lety +11

    The only time JEE level maths was used again.

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

    This explanation helped me to solve another famous problem ''intersecting chords in a cirlce". Thank you tech dose.

    • @techdose4u
      @techdose4u  Před 4 lety

      Welcome :)

    • @vipulsharma7190
      @vipulsharma7190 Před 3 lety

      how did you solve that by using this?

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

      @@vipulsharma7190 Because it can be solved using catalan number method.
      Suppose you a have a circle wihtout any cuts then it is complete 1 unit.
      But when you make 1 cut then circle is distributed in two parts. (1+1=2). Then when you make the second cut total number of parts of the circle will be 2 parts pahle se the + 2 (because this is the second cut) . Now in total you have 4 cuts. And now if you cut the third cut then 4 pahle se + 3(because this is the third cut). So total parts of circle is 7. Again if make the 4th cut then 7 + 4 (4 becauae this is the 4th cut) So total parts of circle = 11.
      And this is how it goes on which can be solved with the help of catalan number method.

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

      @@shresthmishra9329 thanks bro for explanation

    • @shresthmishra9329
      @shresthmishra9329 Před 3 lety

      @@vipulsharma7190 ☺️

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

    This channel is very underrated. Nice explanation!

  • @anitasrivastava983
    @anitasrivastava983 Před 4 lety +5

    The man , the myth , the legend uploads once again!

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

    Such a great video explanation ☺️😊.. thank you sir

  • @kuangyimeng8850
    @kuangyimeng8850 Před 2 lety

    very clear explanation. Thank you.

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

    As always, great explanation

  • @bhavitmathur5476
    @bhavitmathur5476 Před 3 lety

    Very Helpful Videos. Please keep posting

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

    very good video, first time subscriber, want to check out your other videos too. your implementation is Order of n square. Order of n implementation is " long Cn=1;
    for(int n=0;n

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

      In interviews, you can be expected to explain more than one approach. So you should always be prepared.

    • @techdose4u
      @techdose4u  Před 4 lety

      Yes true

  • @aadityakiran_s
    @aadityakiran_s Před rokem

    Hey, your channel is great and your explanations are very nice. I also noticed that your handwriting is very legible. What's the setup you use for whiteboarding? Do you do it using a digital drawing pad?

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

    Very well explained. Subbed!

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

    Brillaint 3xplanation bro!!!

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

    This is very helpful bro ... Thanks a lot..

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

    Thank you by watching this video alone I know how to solve it. What about 95. Unique Binary Search Trees II ? I think it's much harder than this.

    • @techdose4u
      @techdose4u  Před 3 lety

      Yea it must be!! Since you told me 😅 lol

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

    5:21, one small correction, the correct formula will be Cn=∑i=0,n−1 (CiCn−i−1. ) It will go to n-1, not upto n.

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

    You should add this to your tree playlist.

  • @siddhartha.saif25
    @siddhartha.saif25 Před 3 lety +1

    very helpful. thanks

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

    Can you please clear my doubt as to why and how we would have only 3 combinations of 2,3,4 (i.e. c3) in the right subtree when taking 1 as a root? Why won't there be 5 structurally unique combinations at there (for 2,3,4) because we would get 5 as a answer when we have n=3?

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

      Actually C3 means count of no of unique BSTs with 3 unique numbers. It is not an exact value. Think of it as an unknown constant.

    • @meghamalviya8495
      @meghamalviya8495 Před 4 lety

      @@techdose4u ohk..thanks!

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

    Nice explanation sir...

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

    Great explanation

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

    Helpful video 😊 too much for interview
    Thank you always

  • @devanshgoel3433
    @devanshgoel3433 Před rokem

    thank u sir!

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

    Hi, could you please upload a video to generate unique binary search tree in the range [1,n] please

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

    I am viewing the solution directly , i couldn't think of any method to solve it , is it alright or should i change my approch.

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

      If you made no progress in 30 minutes then look at the solution.

    • @techdose4u
      @techdose4u  Před 4 lety +5

      It's alright. You don't have to know everything. Just keep learning new concepts.

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

    thanks

  • @kevinchittilapilly8221

    Can anyone explain why T[0] is taken as 1? If there are no nodes then shouldn't total bst be 0?

    • @calvin2042
      @calvin2042 Před 2 lety

      No structure is a unique structure.

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

    In short, this is the formula:
    Catalan Number = (2n)!/((n+1)!*n!)

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

    class Solution {
    long long dp[100000]={0};
    int catalan(int n)
    {
    if(n==0)
    {
    return 1;
    }
    if(dp[n]!=0)
    {
    return dp[n];
    }
    int ans=0;
    for(int i=1;i

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

    Itna mazaa kyu aa rha haiii😀

  • @pwnweb5734
    @pwnweb5734 Před 2 lety

    AND.. how are we supposed to come up with this formula during interview ? NOP , not intuitive

  • @AmanSharma-or5vh
    @AmanSharma-or5vh Před rokem

    best

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

    first example binary search tree (BST) structure is invalid .
    All nodes in the right subtree of a node must have values greater than the node's value.
    All nodes in the left subtree of a node must have values lesser than the node's value.

  • @rajaganji7982
    @rajaganji7982 Před 2 lety

    when n=4;
    C0=0;
    C1=1;
    C2=2;
    C3=3;
    = C0*C1 + C1*C2 + C2*C1 + C3*C
    = 0*3 + 1*2 + 2*1 + 3*0
    = 4.
    But 4 is wrong. Please let me know if I am wrong.

  • @Official-tk3nc
    @Official-tk3nc Před 4 lety +4

    Great video . I think you are an Indian because no one in youtube could teach better than Indian ...... atleast cse concepts :) ###nitjstudenthere

  • @alirezaamedeo
    @alirezaamedeo Před 2 lety

    5:22 You wrote Catalan number wrong. It should be C(n+1) = Sigma...

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

    jab sb kuch dusre video se hi dekhna tha toh ye video kyu bnaye ??