Table of Contents: The Problem Introduction 0:00 - 1:11 The Placement Intuition 1:11 - 6:56 Introduction To The Transition To Formal Equations 6:56 - 7:18 The Definition For G(n) 7:18 - 7:50 The Definition For F(i, n) 7:50 - 8:44 Applying Our Definitions To Our Previous Intuitions 8:44 - 10:43 The Final Mental Leap: The Cartesian Product 10:43 - 12:44 Time Complexity 12:44 - 13:20 Space Complexity 13:20 - 13:44 The Wrap Up 13:44 - 13:59 Mistakes: 7:18 -> G(n) actually represents the amount of structurally unique BST's we can construct given n nodes. It does not have to be nodes valued from 1...n because values won't matter as long as they are in sequence. (so G(3) can represent 3, 4, 5 or 1, 2, 3 or .... and so on). This is true because the way our possibilities pan out will be the same for the 3 nodes 3, 4, 5 and the 3 nodes 1, 2, 3. The code for this problem is in the description. Fully commented for understanding and teaching purposes.
@AhmedAli-jx9ie4 жыл бұрын
can you write in the video description the online judge problems related to the explanation videos like here is a tutorial about binary tree, go solve these problems to practice what you just learned that's will be more helpful thanks in advance
@BackToBackSWE4 жыл бұрын
Yes we will be implementing this on backtobackswe.com (paywall) soon, we won't go back to the KZbin and do this
@florencechan88635 жыл бұрын
I didn't understand the explanation for the multiplication at first. Maybe it's just me but I don't feel like its well explained -- cross sections doesn't visually tell me anything. So for those who are asking to yourselves, why multiply? Let's take a step back: What is G(1), G(2), G(3), ... G(n) etc. It describes a binary search tree with n nodes (doesn't matter what the root is) and the value it represents is the number of structurally unique binary search trees we could make given n nodes. So let's say we have G(3) 1 3. 2 1 3 2 2 1 3 3 1 3. 1 2 2 Those are the 5 trees. So if we have for example f(3,6) this expands to something like below: f(3,6) / \ G(2) G(3) G(2) looks like this 2. 1 1. 2 So 2 trees Why is it multiplication? Well, let's stop thinking about trees and the tree structure and take a step back further If I give you two arrays and I want to know the number of possible values you can make (doesn't matter if there's repetition) by taking one number from one array and one letter from the second. For example: A B 1 2 3 4 5 Your mind will likely naturally match it up like this: A / / | \ \ 1 2 3 4 5 B / / | \ \ 1 2 3 4 5 Kinda hard to draw with a keyboard, but my point is you will do something like Fix the Letter and match it with every single possible number. So A1 A2 A3 A4 A5 then B1 B2 B3 B4 B5. Now the number we get is 10, which is actually just 2 * 5. If you see the pattern your brain automatically dead when matching it up it said, fix 1 letter for each letter and match it to each number. For every letter we match to N numbers -- and we do this M times --> N is the size of the number array and M is the size of the letter array. So N*M. How is this related to the tree? Same idea but not arrays. You are fixing a root. Not sure if this will work for you but the way it clicked for me was when I visualized the left subtree with a fixed root (any valid fixed root). Then I rotate through all the valid possible right subtree roots. So for one fixed left root, that's rotates whatever number in side G() for the right root. But we do this by the number of valid fixed roots on the left subtree which is also whatever number inside G() for the left root. Now I hope you see why it's multiple. It's just permutation in a slightly wonkier way. Just to be even more clear for f(3,6) in the example I give earlier it'll start out like this: Left subtree possibilities: Remember G(2) = 2 2. 1 1. 2 Right Subtree possibilities Remember G(3) = 5 1 3. 2 1 3 2 2 1 3 3 1 3. 1 2 2 So on the left is 2 match it with 1 on the right 1. 2 3 Fix the two rotate the right to all the possible right tree possibilities. By rotate, I mean use the different right tree possibilities (I may have been unclear on that). So: Left Right 2 3. 1 2 1 Left Right 2 2 1 1 3 Left Right 2 1 1 3 2 Left. Right 2 3 1 1 2 So that's 5 possibilities already. But left can be 1 2 as well, so repeat the whole process with it fixed at 1. That's a total of 10 possibilities. i.e 2*5. Remember G(2) = 2 and G(3) = 5. Well G(2) * G(5) = 10. It's not magic that it all works out at the end. Just a sprinkling of permutation :) I kinda feel dumb for not noticing this at first. It just goes to show that sometimes we have to take a step back and not be so focused on what's in front of us (the tree and tree structure) and think of the bigger picture which is really just a permutation that our brain can naturally handle with arrays.
@BackToBackSWE5 жыл бұрын
yeah this was one of my early videos, getting better every video, thanks for this - hope it helps someone reading this
@vincentkoo44535 жыл бұрын
This post helped me a lot, thank you very much
@BackToBackSWE5 жыл бұрын
@@vincentkoo4453 nice
@eug_gino5 жыл бұрын
this was a beautiful explanation, you should def try to make some leet code tutorials sometime
@BackToBackSWE5 жыл бұрын
@@eug_gino I support
@SamM-ne5uq5 жыл бұрын
You do not give out just solution to the problem but explain how to develop the final solution. It is simply awesome. Thank you!
@BackToBackSWE5 жыл бұрын
sure
@kevinnguyen93975 жыл бұрын
Thanks for this! I was actually doing this problem the other day, and had trouble with the intuition and had to step away. You definitely cleared it up for me.
@BackToBackSWE5 жыл бұрын
That is AWESOME. This is what I'm all about. I've had hundreds of those "step away" moments and that is what inspired me to make explanations myself since I felt others didn't explain it the way my brain "taught me" if that makes sense. This is my goal for this channel. Thank you.
@yousufhussain95303 жыл бұрын
Same here. I looked at a few solutions and couldn’t really understand them. Thinking of dividing the solution space as a cloud was a lightbulb moment. Thank you! 🙏
@ankitgoyal85564 жыл бұрын
Hey, u are compelling me not to mug up. The first time I feel like this video is good, the second time I feel like this is out of the world. Thank you so much, brother.
@BackToBackSWE4 жыл бұрын
great and tahnks
@yuvrajjagga93515 жыл бұрын
I tried for around 3 hours on this problem. It was strange because I could analyse the problem and it's requirement but I could not ,like you said, materialize the logic into a recursive programming solution, only to find out, after watching this video, that it lies in Dynamic programming zone. A great explanatory video.
@BackToBackSWE5 жыл бұрын
I did the. same, I didn't get this first time I did it
@snowy0110 Жыл бұрын
It's rare to find good explanations. Yours is simple and I appreciate it. Thanks!
@soymaxxing5 жыл бұрын
Great explanation, I was having trouble visualizing how this problem had an optimal substructure. But ur explanation from how G(n) related to F(i, n) really helped me see it! Thanks
@BackToBackSWE5 жыл бұрын
sure
@aynurmukhambetova17084 жыл бұрын
i havent even watched it to the end but everything got so clear! thank you
@BackToBackSWE4 жыл бұрын
great
@tumul14744 жыл бұрын
I didn't even understand that it was a dp prob but the way u explain it man, I was able to deduce the answer around 9:00...thanks a bunch !!
@BackToBackSWE4 жыл бұрын
great to hear
@jishulayek82525 жыл бұрын
You are an amazing teacher !!! Loved it
@BackToBackSWE5 жыл бұрын
thx
@dipinanda1Ай бұрын
wow man, amazing. U made that look so easy
@amitmehta22852 жыл бұрын
I love you you helped me really
@BackToBackSWE2 жыл бұрын
Thanks buddy! keep hustling
@waleedahmed17254 жыл бұрын
I stumbled onto this video while scratching my head on this problem on leetcode: leetcode.com/problems/unique-binary-search-trees/, and wow I just got to say your explanation was crystal clear and might be one of the most complete I've seen for any leetcode problem that I've gone googling. Glad I stumbled onto your channel!
@BackToBackSWE4 жыл бұрын
Thank you. Welcome to the channel.
@maripaz56504 жыл бұрын
What a beautiful explanation. Thank you! I had a light-bulb moment when you were laying out how to solve n=2 and n=3.
@BackToBackSWE4 жыл бұрын
great
@Oleks3805 жыл бұрын
Thank you man! You're doing a great job
@BackToBackSWE5 жыл бұрын
sure
@markgraham23123 жыл бұрын
It was a good explaination. Thank you.
@girishthatte4 жыл бұрын
Perfect and concise explanation
@BackToBackSWE4 жыл бұрын
thanks
@TheGizmoTruck4 жыл бұрын
year later, this is still helpful. Thank you
@BackToBackSWE4 жыл бұрын
dang...it's been a year. sheesh. And sure
@כפירכהן-נ8ע4 жыл бұрын
i comment here so the youtube algorithm will pick this video, amazing video, well explained
@BackToBackSWE4 жыл бұрын
thanks
@annettejohn68293 жыл бұрын
Thanks a lot! was struggling with the intuition for this and now it makes a lot of sense!
@ivantishchenko46864 жыл бұрын
Superb explanation!
@BackToBackSWE4 жыл бұрын
thx
@nirankarojha26604 жыл бұрын
I love the explanation of the solution. Thanks a lot.
@BackToBackSWE4 жыл бұрын
sure
@Gabriel-ms6qw4 жыл бұрын
I think a better explanation would be dp[i] are all the ways you can create a binary tree using "i" elements.Lets say you have n = 6, when you root 4, in the left subtree there are only 3 elements less than him so there are dp[3] ways you can arrange those in a binary tree and 2 elements greater than 4 (5,6) so there are only dp[2] ways you can arrange them. Now, For every way you can arrange the left subtree you have dp[2] ways you can arrange the right one, so basically its dp[2] + dp[2] ... + dp[2], this is the multiplication.
@BackToBackSWE4 жыл бұрын
Yes, thank you
@ZackOfAllTrad3s5 жыл бұрын
For those still having trouble I would suggest reading the leetcode article after watching this video, they work well together.
@BackToBackSWE5 жыл бұрын
yeah
@Rahul-uk4su4 жыл бұрын
This was super helpful thanks. I liked the video
@BackToBackSWE4 жыл бұрын
great
@fastisslow61774 жыл бұрын
Nice and powerful explanation.
@omnya93814 жыл бұрын
Amazing explanation!!! Thank you so much
@BackToBackSWE4 жыл бұрын
sure.
@OP-yw3ws Жыл бұрын
Thanks for the awesome explaination!!!
@nithishkumar25614 ай бұрын
Great explanation bro
@jianxiaohu82135 жыл бұрын
Thank you, that was so well explained!
@BackToBackSWE5 жыл бұрын
sure
@chintangotecha70895 жыл бұрын
Great explanation. Highly appreciated :)
@BackToBackSWE5 жыл бұрын
sure
@punitpawar42314 жыл бұрын
Will you be able to put up a video that returns a list of the unique binary trees instead of the count. Given an integer n, generate all structurally unique BST's (binary search trees) that store values 1 ... n.. Thanks a lot !!
@BackToBackSWE4 жыл бұрын
We don't have that yet yes, I think we do have it on backtobackswe.com (paywalled)
@kxiong40214 жыл бұрын
Thanks for the explanation!!
@BackToBackSWE4 жыл бұрын
sure
@nishanth9984 жыл бұрын
your teaching is awesome...!!! plz tell where the code is present...
@BackToBackSWE4 жыл бұрын
The repository is deprecated - we only maintain backtobackswe.com.
@jinhopark36713 жыл бұрын
Great explanation. Thanks!
@gokulgopinath37773 жыл бұрын
Great explanation
@vedankgoyal17064 жыл бұрын
If we use Dynamic Programming our time complexity will be O(n^2) but if we use maths it will become O(1) as the direct formula for Catalan number is 2nCn/n+1. But since it is a programming question DP approach should be known I guess.
@BackToBackSWE4 жыл бұрын
ye
@persianwaffle4 жыл бұрын
hey vedang, would you mind sharing a link or something where it is being calculated in o(1).
@vedankgoyal17064 жыл бұрын
@@persianwaffle brilliant.org/wiki/catalan-numbers/ We can use the formula for Catalan number
@jayeshmarathe76783 жыл бұрын
video on Optimal binary search tree please
@VijayKumar-bp1fp4 жыл бұрын
very good explanation...
@BackToBackSWE4 жыл бұрын
thanks
@aikolkoikelov75143 жыл бұрын
Thanks man for a such clear explanation )
@BackToBackSWE3 жыл бұрын
Happy to help!
@vivil79173 жыл бұрын
Thank you so much for the explanation, very clear and very accurate!
@rohan8arora5 жыл бұрын
You're an amazing teacher man. I wonder what's your tech stack in backtobackswe website? Its quite nice.
@BackToBackSWE5 жыл бұрын
We are using Next.js rn, but we are rewriting the whole thing and creating a new system that will be out in 3-6 months.
@zzzz265263 жыл бұрын
at 8:39 wouldn't the number of nodes you have to work with be 2? if you plant 1, 2 , 3 at the root, do you include the root in the number of nodes you have to work with?
@jyotisingh81834 жыл бұрын
Where is the code in description ?? Thank you for the video.
@BackToBackSWE4 жыл бұрын
Hi, the repository is deprecated - we only maintain backtobackswe.com now
@TheSteak19843 жыл бұрын
Big Thank You!
@karansagar78704 жыл бұрын
Gr8 explanation
@BackToBackSWE4 жыл бұрын
thx
@kunqian065 жыл бұрын
Thanks for the awesome explanation
@BackToBackSWE5 жыл бұрын
sure
@Code4You12 жыл бұрын
Thank you bro!
@chris.w3913 жыл бұрын
Best explanation
@RodrigoVillarreal5 жыл бұрын
The video is fantastic, however I got a bit confused about why do we multiply instead of sum?
@BackToBackSWE5 жыл бұрын
thanks! and can you timestamp the part that confuses you (it saves me time because I respond to comments in big batches)
@harsaroorabhyankar5 жыл бұрын
since both left and right side are individual and are not related to each other, for each left BST we'd have multiple right BST and vice versa. We need to report the combination of both of them so it's multiplication.
@sudhakar1046245 жыл бұрын
Awesome explaination
@BackToBackSWE5 жыл бұрын
thx
@mr.mystiks99684 жыл бұрын
Skip to 7:00 if u already know the basics.
@BackToBackSWE4 жыл бұрын
Ok
@taiwan.15 жыл бұрын
do you have another video that can explain why F(i, n) = G(i-1) * G(n-i) ? any videos for a similar question that may help explain this?
@BackToBackSWE5 жыл бұрын
Haha yeah this may not have been enough, I'll recover this question soon
@taiwan.15 жыл бұрын
I got it now.. kinda..... However, Thank you so much for all these videos that you have made. They are all amazing.
@BackToBackSWE5 жыл бұрын
@@taiwan.1 No, you have to get it 100%, that's my job. And sure, I dropped the ball here, I'm releasing a course w/ Chris Jereza where I won't let explanations be subpar
@weibangzhang39944 жыл бұрын
great work
@BackToBackSWE4 жыл бұрын
thx
@大盗江南5 жыл бұрын
Thank u super much man!
@BackToBackSWE5 жыл бұрын
ye
@motivation_hubPJ4 жыл бұрын
Thanks a lot bro
@BackToBackSWE4 жыл бұрын
sure
@christiansakai5 жыл бұрын
Hey man, I have a request. On Leetcode there are three templates on Binary Search. It would be great if you do a series of explanation for that!
@BackToBackSWE5 жыл бұрын
Very, very, fascinating. I have a video coming out in a 2 or so days (pre-recorded since I'm not in the USA right now) on a binary search problem and I have a code sample for it. It is a Facebook interview question I got first round (I passed 1st round, failed 2nd round although I knew both questions...long story). I approach it with Approach #1 with some tweaks. In my opinion, you only need to know Approach #1 and then every binary search problem will be a tweak of it. I think the whole "templates" concept is a bad idea not because it is not brilliant or well thought out, but because it causes you to MEMORIZE things. And I'm not a fan of that especially for these kinds of questions. (Also a long as the search is truly a binary search, its asymptotic nature will be logarithmic. Some tweaks on approaches will improve wall time but not time complexity) I'm all about intuitions and knowing HOW and WHY a problem is solved a certain way and with binary search, it is all about narrowing search space using a certain set of deductive logic at each step. Anyway, you'll see what I mean when I put that video out. We will do a great amount of binary search this year with many variants.
@christiansakai5 жыл бұрын
@@BackToBackSWE Thanks man! I appreciate your time putting videos like these out. It must be a ton of work! Yeah I've been trying to understand the templates thingy to no avail, since there is no explanation on that article on Leetcode. Currently my biggest weaknesses are Binary Search, Backtracking and DP. DP is just so hard to do. I tried various approaches, and like you said, probably I just need to expose myself to more problems on that nature.
@SidharthJhawar5 жыл бұрын
Subscribed!!!
@BackToBackSWE5 жыл бұрын
hey
@zzzz265263 жыл бұрын
So at 10:58 G(0) does not yield any subtrees (if you have no nodes to begin with it's impossible) but you say it yields 1 because the result of the original problem is the cross product of the left and right side. So although G(0) is technically 0, you say it is actually 1.
@alanliang95384 жыл бұрын
good tutorial! (finally a tutorial with no indian accent)
@BackToBackSWE4 жыл бұрын
hey
@alirezaRahmanikhalili5 жыл бұрын
good job
@BackToBackSWE5 жыл бұрын
thanks
@shubhamsinnarkar29233 жыл бұрын
Where is the code for this?
@stonecoldcold29414 жыл бұрын
code is not in the description!
@BackToBackSWE4 жыл бұрын
The repository is deprecated - we only maintain backtobackswe.com now.
@lewisben66975 жыл бұрын
If I use vector(ArrayList) instead of an array, it still costs O(n2) time or not?
@BackToBackSWE5 жыл бұрын
not sure?
@jerrywu57975 жыл бұрын
Highly appreciate your in-depth explanation! But I have trouble understanding the basic case when G(0) = 1. Given the description from Leetcode "Given n, how many structurally unique BST's (binary search trees) that store values 1 ... n?", n should not be zero, but in this solution it's a must to set G(0) = 1, or we cannot take the case when child node = nullptr into consideration. Is there any way to understand this? (It also occurs that for other dynamic programming problems, to set the base case value also bothers me!) Also, I am currently in the same situation as you are. I failed the final round interviews from Facebook and Bloomberg, which both are my dreamed companies. This is super frustrating but I have to keep working on these problems, because I believe with a deeper understanding of OOP design, algorithms and a bit of luck, I will get a job. Currently I started from scratch, reading the book Cracking the Code Interview and practice each problem with the help of Leetcode.( There are sometimes better solutions at Leetcode discussion tab than the ones given in the book!) Anyways, Thanks for your video. Best luck!
@BackToBackSWE5 жыл бұрын
Yes. Ok, so when the problem says numbers from 1 to n it is has the hidden stipulation that n >= 1. What happens when n == 0? How do we adapt the description to that? Well if n == 0 then...we can't do the enumeration from 1 to n and therefore we have to implicitly understand that...if n == 0 then we will have no nodes to work with for out placements. No enumeration, no nodes. This is not just randomly saying G(0) = 1. When n is 0...we have no nodes to do anything with. What can I give back to you? Nothing. An empty tree. That IS SOMETHING. Therefore, when n == 0 we know that we will make 1 and only 1 unique tree...the empty tree. Does this clarify? Not sure if that answered your curiosities.
@jerrywu57975 жыл бұрын
@@BackToBackSWE Thank you for your quick reply! Yes i understand now. Considering the fact that when we do the calculation, we need to treat an empty tree as something exists. Therefore, by multiplying 1, it actually is one of the case s for all possible combinations(that is, left child node or right child node doesn't exist). Crystal clear now! Will check your other videos!! Thx my friend.
@BackToBackSWE5 жыл бұрын
@@jerrywu5797 ok good. Yeah I'm on my laptop working on the channel/website/content/planning/a lot more, hence the quick reply.
@heisenberg18443 жыл бұрын
Could you please do part 2 of this question, leetcode.com/problems/unique-binary-search-trees-ii/ where we are supposed to actually generate the BSTs. Thanks.
@ky50695 жыл бұрын
Serious remark: I've noticed your itching throughout the videos, so just a friendly reminder that if you have an itch in the entire body, you might want to check if you have habits that might overload your kidneys (food, activities, etc).
@BackToBackSWE5 жыл бұрын
I have Eczema and the rooms at the library I shot videos at got hot. Weird comment to make.
@CostaKazistov2 жыл бұрын
1:39 made me laugh 🤌
@swatigojra79044 жыл бұрын
Everywhere this udemy ad is frustrating
@BackToBackSWE4 жыл бұрын
lmao - I'm sorry. We will maybe remove ads when we hit 100k subs
@ankitgoyal85564 жыл бұрын
How to like video again ??
@BackToBackSWE4 жыл бұрын
hack the API
@ankitgoyal85564 жыл бұрын
@@BackToBackSWE😂😂
@sahil65674 жыл бұрын
possibilities
@BackToBackSWE4 жыл бұрын
yes.
@kanav515 жыл бұрын
Never thought that non Indians can make cool programming tutorials lmao.
@BackToBackSWE5 жыл бұрын
hahaha, well they can now
@sudheer-suri5 жыл бұрын
The way you explain is no where intuitive , it sucks bruh!
@BackToBackSWE5 жыл бұрын
ok
@mr.mystiks99684 жыл бұрын
Initially it seems like the equation comes out of nothing but it actually is intuitive. You have to consider the question, if X is between 1-N, and I choose X to be the root then how many choices do I have for the left and right subtrees. Left subtree is 1 to (X-1) options. Right subtree is (X+1) to N options. The only thing that may be unclear is why we multiple the two G functions but if you try N = 3 and X as any value from 1 to N, then it becomes clear they must be multiplied else u get an undesirable answer. This is ultimately a counting problem and you have to question what your options are, notice a pattern between the options for the left and right subtrees, and write out a function to generalize it.