• 1 day ago
Transcript
00:00Next question, which we are going to talk about is size of largest binary search tree in a binary tree.
00:06I have a given binary tree and in this BT, I have to find out my largest binary search tree
00:13and return its size. If I talk about this particular tree, then if I think that my whole tree is
00:22a BST, then the condition is invalid. Why? Because there is 50 root node, which is left of root node.
00:29Left child that is equal to 30. These two nodes are satisfying BST condition. These two nodes are satisfying BST
00:35condition. But this node number 20, this is my 30 right subtree.
00:42That's why I don't satisfy this BST condition, so my whole left subtree doesn't satisfy this BST condition. That's why
00:51I won't include my root node. If I talk about 60, 60 left child is 45 and right child is
00:5870. 70 left child is 65 and 65 is also greater than 60.
01:0370's right child is 80. So, the tree is so much, it satisfies my BST condition completely. This is why
01:12this is a BST and in fact it is my largest BST. In this whole binary tree, the largest BST
01:21form could be this BST. So, the number of nodes will be size, which is 1, 2,
01:263, 4 and 5 equal. So I will print my value 5 in the answer. So what do I do?
01:32I will have a given binary tree in which I have to calculate the largest possible BST size. Now, for
01:38solving this question, we will use the same approach in which we have in this valid BST.
01:46We had a question in which we had a big binary tree and we checked whether the binary tree is
01:53a valid BST or not. What do we do? Just change the same question, just a little bit of a
01:59form and we will find out the answer to this question. To check the valid BST, what did we do?
02:06First of all, we saw that if a root
02:11If the root node is null, it will always be a valid BST. If the root node is null, it
02:15will always be a valid BST. If the root node is null, it will always be a valid BST. Second,
02:22we track min and max. Min was the minimum possible value, max was the maximum possible value. If my left
02:34subtree
02:35I will remove min and max and remove my right sub-tree from min and max. So I can recognize
02:43that my root node will satisfy my BST condition or does not satisfy my BST condition or does not satisfy
02:49my BST condition. Let's take a look at my minimum 5 and maximum 30 or equal. Here the minimum that
02:57is equal to 45 and maximum that is equal to
03:00Now I will compare this node to my left subtrees to the maximum. If my root node is left subtrees
03:09to the maximum, then it is valid BST. And if my root node is right subtrees to the minimum,
03:18So this is a valid BST. But in any case, if my root node left sub-tree from maximum to
03:25small, then it will be a invalid condition. And if my root node right sub-tree from minimum to small,
03:32then it will be a invalid condition. In two cases, I will return false. When my root node is less
03:40than my max of left, or my root
03:46greater than my min of right.
03:50So what did we do in these two conditions? What did we do in these two questions? We returned false.
03:57So I had a condition that I had no root. Then what did I do? I checked on my left
04:01subtree and right subtree that they are valid. If both are valid and my condition is not true, then my
04:10root is valid. But if my left subtree has invalid, then my root will be invalid. Or if the right
04:16subtree has invalid, then this node is invalid.
04:19Invalid ho jayegi, par dhonho ne true return kiya, phir mein in conditions ko check karungi, agar yeh dhonho bhi
04:25true ho gai, toh mein false return kar dhungi, par agar yeh true nai hoi, us case mein mein kya
04:29karungi, us case mein mein return kar dhungi true value ko, matlab meri paas jo conditions aati thi bst ki,
04:35wo merah valid bst ben jata tha, iske saath saath eck additional cheez hum beech mein karthethe, additionally hum yeh
04:40check karthethe ki, agar meray root ka joh data hai, that is greater than meray left ka data, and meray
04:47root ka joh data hai,
04:49That is less than my right child's data. In this condition, there was a true case. So we handled about
04:564 cases. 1, 2, 3 and 4, which was my final case. The combination of these 4 cases was my
05:04is-valid BST question solved. Now using these 4 properties or conditions, we will find out this approach in which
05:14we will find out the largest BST.
05:16The approach is that if I ask all the subtrees from my own subtrees, which are 4 information? One, I
05:24ask that my subtrees are BST or not. In case, I will get a boolean value. The other thing, I
05:31ask that my subtrees are the size.
05:34The third thing I would like to ask, what is the minimum possible value in the sub-tree? And the
05:39fourth thing I would like to ask, what is the maximum possible value in the sub-tree? If each sub
05:44-tree gives me this 4 information, then I can check that if there is a BST form, then what is
05:51the largest possible size of BST? For example, let's say these 4 information I have taken, and I have taken
05:59a class of object in the form. This is the information class.
06:04So every node will return its information. First of all, what will 50 do? 50 will try its information on
06:10the left sub-tree. So 50 will try its left child. Then 30 will try its left sub-tree. So
06:215 will try its information on the left sub-tree. 5 will return information on the left sub-tree. 5
06:24will return information on the left sub-tree. 5 will return information on the left sub-tree. 5 will return
06:26information on the left sub-tree.
06:31left child is null, right child is null. If any child is null, 5 is also left child. If any
06:37root node is null, then null node always returns true in BST. So null node will return true in this
06:45BST. So if I have a null node, then null node is BST always
06:52true return, null node size is equal to null node. Minimum value will be positive infinity and maximum value will
07:01be negative infinity. I can take 0-0, but in case BST has a negative integer, that's why I have
07:08positive infinity and negative infinity. So, if you compare it with anyone, then it will be stored in minimum value.
07:16Because minus infinity is bigger. So, if you compare it with zero, then it will be stored.
07:22plus infinity to this logic. I have here positive infinity, integer dot max value, and here negative infinity, integer dot
07:31min value. Now, null value will return these values. So, left from 5, right from 5. 5 will evaluate your
07:40value. 5 will return true to this BST, that 5 is a binary search tree form.
07:47What will be the size? 5 will be the size of 1. What will be the minimum possible value? What
07:51will be the maximum possible value? 5 will be the maximum value of 5. What will be the 4 values?
07:58This information will return 30. Then what will be 20? 20 will be the same information. 20 will be the
08:04true value of BST. Size will be 1. Minimum value 20. Maximum value 20. What will be 30?
08:13This information has come and this information has come. So, 30 will use these two information to calculate your information.
08:22First of all, the work is to calculate 30 if it is BST or not. So, first of all, we
08:27will calculate is valid BST all the conditions. We know that this is valid BST check for 2-3 conditions.
08:35First of all, check if root is null. Second condition is to check if my left
08:43maximum greater than from root or my right minimum less than from root. In that case, I will return false
08:54because these two conditions, BST's condition, are being invalid. Meaning, if I analyze 30 here, 30 will be left to
09:035, 5 will be left to max, 5 will be returned to max. So, if the value of 30 is
09:08greater than 5, it will be true.
09:11But if you check the small condition, this condition will be false because the value of 30 is greater than
09:155. So in this case, this will be false. Then we will check if the right minimum is less than
09:21root. The right is 20 and the right minimum is 20. 20 is less than 30. So this condition will
09:28be true. If it is true, we will return false. I will make this BST.
09:38false ke equal. So, 30 ke andre information return hooghi. Uusmei is BST ki value false a jaeegi. Ye size
09:45kya return kareega? Size jo hooga, woh size 30 aapna aur aapne saare subtrees ka mila kar dega. To kisi
09:52bhi node ke lihe, uska size ho jaeega. Left ka size plus right ka size plus one ke equal. Yarni,
10:01pure left subtrees ka usmei size add kar diya, right subtrees ka size add kar diya or usmei add kar
10:05diya khud ka size. So, it will be equal to one
10:07plus 1, plus 1. Yarni size yahan par ho jayega 3 ke berabar. 30 ke liye joh minimum value calculate
10:14hooggi. Matlab third information mein joh usko minimum deena padeega. Ye joh minimum value hooggi. This will be minimum of,
10:21ya to root ka data minimum ho jayay. Ya phir, mire left ka minimum compare karlay hum, right ke minimum
10:28ke saath. Tino values ko mujhe compare karna hai ki mire root mein.
10:32In my left minimum and my right minimum, which is less? Here, left minimum will be 5, right minimum will
10:42be 20, root data will be 30, so which minimum will be 20. Here, I will store 20 and maximum
10:48of root data, left maximum and right maximum. Left maximum is 5, right maximum is 20, root data is 20.
10:56So, here maximum value return will be 30.
11:00because my root data is 30 which is greater than these two. So what will you do with 30? You
11:05will return this information to 50. What information did you return? That 30 is not a valid BST. 30 size
11:11is equal to 3, meaning the whole 3 size. Minimum value is equal to 20 and maximum value is equal
11:17to 30. So this data will return to 50. I will remove this for clarity.
11:22Now in the same way, 50 has left sub-tree, now that's right sub-tree. In the right sub-tree,
11:29this node will return true, because this is BST, then it will return 1 to size. Minimum will return its
11:37value, maximum. In this way, 65 will return true, size will return 1 to size. Minimum will return its value,
11:43maximum.
11:45Now, what will you do for 70? What will you do for 70? It will be 65 and 80. So,
11:49you will calculate your information. You will see if this is valid BST. 70 will check whether its value is
11:5565 maximum. In that case, it will be true. Then, 70 will check whether its value is 80 minimum. In
12:03that case, it will be true. This value will be false. So, my tree will become a valid BST. The
12:11size of it will be 3.
12:133. The minimum possible value will be 65, maximum will be 80. So 70 what will do? I will return
12:20our information to 60. I will start the information for clarity. So 60, 45 will return to this BST. Size
12:31will return to 1. Minimum value will be your value. Maximum value will be your value.
12:35So 60 passed on your left sub-tree and on your right sub-tree. Now 60 will calculate its own
12:41information. For your information, what will become 4 values? For 60, we will see if 60 is a valid BST.
12:50In this case, we will return all the conditions in this valid BST. We have studied these conditions in this
12:54valid BST. When we write code in more detail, we will understand how this valid BST is going to be
13:01checked. So in this valid BST is true. What is the size?
13:103 plus 1 plus 1 which is equal to 5. So size me 5 return minimum what will happen? Minimum
13:17will be 45. Maximum what will happen? 60 return maximum 60 return will be 80. So for 60 to calculate
13:23the information.
13:2460 as this information is calculated, the information will go to 50. 50 from left and right from left. What
13:33will 50 do? 50 will calculate its data. 50 is SBST? 50's left child already has false in this SBST.
13:41So, 50 will never be able to store the false. What will the size be? 3 plus 5 plus 1
13:48which is equal to 9. Here,
13:51Left we have minimum value that should be 5. Left will give 5 and maximum will give 30. So minimum
13:59value will give 50 and maximum value will give 80 equal. Now notice here that with the biggest size, which
14:07is SBSD or which is SBSD, which is SBSD, which is SBSD. The biggest size is 5. So we will
14:16take a static variable. Maximum size.
14:20or size of BST, which we will put 0 in the beginning. And this will compare every single person with
14:27the size information. Where the size of BST is the maximum size of BST, then we will update the static
14:35value. First, the size of BST is equal to 5 and the value of BST is true. So the size
14:42of BST
14:43It will be updated in 5. The largest BST is 5 size. When I will remove 50 value in 50,
14:50the size will be 9, which will be bigger than this size. But here, this BST is false. This is
14:56not a BST. Therefore, we will not update this size here. And the maximum possible BST size will be equal
15:02to 5. Now, I will summarize this whole process. Actually, what are we going to do? For each single node,
15:11We will calculate this information on this basis.
15:17We will check what the valid BST is, we will calculate the size and we will calculate the minimum and
15:22maximum possible value. So what will I do? I will call for left child. I will find the left information,
15:30then I will find the right information. Then I will create the self or current information from it and we
15:37can find out and compare it with maximum size.
16:07in a binary tree.