• 2 days ago
Transcript
00:00Next question which we will talk about is validate BST. It is a very famous question.
00:04Snapdeal, Amazon, Facebook and also many companies have come in this question.
00:09What do we have to do in this question?
00:11The question is that we have given a BST and we have to know whether it is a valid BST
00:16or not.
00:18Valid BST means binary search tree's properties, every single node,
00:23whether it is a valid BST or just BST and actually doesn't satisfy properties.
00:30Now, the first thing to solve in our minds is that we compare the root to the left node
00:36and compare the root to the right node.
00:40And if this is possible for all of us, then we will have a valid BST.
00:46But which case fails in our case?
00:49This case fails in which we have root node 3.
00:52Then we are doing some comparisons with 2 and 1.
00:56And in the right sub-tree we have 5.
00:58So what do we do in this case?
01:00If we follow this approach, then simply what do we do?
01:04We compare 3 to 2, it's big.
01:053 to 5, it's small.
01:07So this is ticked.
01:09Then we compare 2 to 1, it's small.
01:112 to 4, it's big.
01:12So this is also ticked.
01:13These are leaf nodes already ticked and 5.
01:16So while following this approach, this will be a valid BST.
01:20But this is actually a valid BST.
01:23Why not?
01:24Because node 4 is bigger than 3.
01:27But where is it?
01:29It's in the left sub-tree.
01:32Which is the property of BST.
01:35All of the left sub-tree nodes are smaller than root nodes.
01:38So this node should not actually be here.
01:42It should be in the right sub-tree.
01:45So just to compare our immediate left child or immediate right child to our right child.
01:51We have to compare the left and right sub-tree to the right sub-tree.
01:55And how can we compare it?
01:58So it comes to our approach 2.
02:00Now, approach 2 is a very simple approach that we can solve this question.
02:05The approach is that if anyone has to see BST, it's valid or not.
02:09So remove the in-order traversal.
02:12If the in-order traversal is sorted, it will be a valid BST.
02:17Like what is in-order traversal for this tree?
02:21In-order traversal is 1, 2, 4, 3 and 5.
02:25Which is not sorted.
02:27So how will we compare the in-order traversal sorted?
02:30For each key, we will see what the next value is greater than it.
02:34If there are large values, it is sorted in increasing order.
02:38So our tree is valid.
02:41So this is the first way to check validity.
02:44This is the second way to check validity.
02:46We also have the maximum value of the left sub-tree.
02:50And remove the right sub-tree minimum value.
02:54For each single node.
02:55How will we do this?
02:57For example, if I have the same example loop for the previous one.
03:01In which we have 3, then 2, then 1, then 4, then 5.
03:06What is my job?
03:08To remove the left sub-tree maximum value.
03:12To remove the right sub-tree minimum value.
03:15To remove the right sub-tree minimum value.
03:16This maximum value can also be called in-order predecessor.
03:22I mean, the first thing comes from predecessor.
03:25This is probably the same spelling.
03:27And what can we call this?
03:28We can call this minimum value in-order successor.
03:33Which we have talked about.
03:34So minimum value in the right sub-tree and maximum value in the left sub-tree.
03:39With this value, I have to compare my root node.
03:43Left child and right child's value are not.
03:46With my maximum in the left sub-tree and minimum in the right sub-tree.
03:50If we have a comparison with the root node.
03:52With each single root node.
03:55If that condition is satisfied.
03:57Then in that case, my binary search tree is a valid BST.
04:01So what do we have to do?
04:02We have to check that maximum value in the left sub-tree is always less than node.
04:06And minimum value in the right sub-tree is always greater than node.
04:10If the maximum value in the left sub-tree is less than node.
04:14So the other sub-tree will automatically be less than node.
04:17And if the minimum value in the right sub-tree is increased.
04:19then the other sub-tree will automatically increase.
04:21So for this we have to write code.
04:23Now the values, we will write code in this way.
04:26So we have to write a special function.
04:28We have to write them directly with calls.
04:31So what do we do?
04:32We will use a very simple approach for solving this question.
04:36For example, we have to take a binary search tree.
04:39Here comes 5.
04:41Here comes 3.
04:43Here comes 1.
04:45Here we have 4.
04:46Here we have 6.
04:48Here comes 7.
04:50Now we will see this for each node.
04:52That we should put the minimum and maximum value in this range.
04:56Now we know that if this is our root node.
05:00Then in the left sub-tree.
05:02In this sub-tree, all the values of this sub-tree
05:12And if we talk about the right sub-tree,
05:14Then all the values of this sub-tree
05:16We should take our infinity to 5.
05:20That means, the values of this sub-tree
05:25The values of this sub-tree
05:28The values of this sub-tree
05:30The values of this sub-tree
05:42In the left sub-tree
05:44So, the values of this sub-tree
05:46So, the values of this sub-tree
05:47The values of this sub-tree
05:47The values of this sub-tree
05:48And all these values are 5 and 3
05:49The values of this sub-tree
05:50So, for each node
05:51We know that
05:53What can be maximum and minimum value
05:55So, here we define two variables
05:57We will define the values of this sub-tree
06:12Why?
06:12Because there is no limit for 5
06:14There is no limit for 5
06:14There is a root of the whole tree
06:15So, the values of this sub-tree
06:17Now, we will initialize the left sub-tree
06:20For 3, the minimum value
06:22That is equal to null
06:24And the maximum value
06:25That is equal to 5
06:27Why?
06:27Because 3 is in the left sub-tree
06:30So, whenever we talk about the left sub-tree
06:33When we start calls
06:35Then, our minimum
06:41So, the maximum
06:42That is always going to be equal to the root
06:45So, the root
06:46Which was our parent
06:49We will always have to be small
06:50If we put it in the left sub-tree
06:52So, if we talk about the left sub-tree
06:55Here, if we talk about 1
06:56So, the minimum of 1
06:58That is always going to be equal to the root
06:59But the maximum of 1 is always 3
07:01Because 3 is the parent of 1
07:04So, the value of 1 is always small
07:06Here, if we go into the right sub-tree
07:09Now, if we talk about 4
07:11If we talk about 4
07:11Then, the minimum value
07:12Will be equal to the root
07:13That means 3
07:15And the maximum value
07:17We know before
07:17Which should be 5
07:19How did this change happen?
07:21Because we are in the right sub-tree
07:22So, the condition of the right sub-tree
07:25Will be in the right sub-tree
07:27In the right sub-tree
07:28In the right sub-tree
07:29The minimum
07:29Will be equal to the root
07:31Or the parent
07:34And the maximum
07:36Will be equal to the root
07:38That will be equal to the root
07:38I mean the maximum
07:41It was 5
07:42That will be 5
07:44And the left sub-tree
07:45In the right sub-tree
07:46The maximum was null
07:47In the right sub-tree
07:50That will be null
07:59Now, let's talk about the right sub-tree
08:04If we talk about 7, we should have minimum value 6 for 7 and maximum value can be something for
08:10this
08:10So, as we traverse the child, we will update the maximum and minimum
08:16If the root node, which node we analyze, we will put our limit in our limit
08:23So, our BST is a valid BST
08:26Where the maximum limit is violated, our BST will invalidate
08:32Now, let's write the code
08:33What do we do in the code?
08:35We call public static boolean is valid BST
08:42We will pass our root in this BST
08:45And check whether it is a valid binary search tree or not
08:50In the beginning, what will we do with root?
08:53One will be the minimum node and the maximum node
08:58In the beginning, the minimum is null and the maximum is null
09:02Now, let's check some conditions
09:05If our root value doesn't exist, if the root is null
09:10Then a binary search tree, which is not a node
09:13That will obviously be a valid BST
09:16So, in this case, we will return true
09:20Now, let's do comparisons
09:22Now, let's do comparisons
09:23So, if at any point, our minimum is not equal to null
09:28Not null is minimum
09:30And our root data is greater than or equal to
09:39So, in this case, what will we do in the case?
09:40In this case, we will return false
09:43This condition should go on and be less than or equal to
09:47So, the minimum data is less than or equal to
09:51and our BST is invalid
09:53But what will be the other condition
09:55What will be the next condition?
09:56If maximum is not equal to null
10:00And our root data is greater than or equal to maximum data
10:06What will happen in this case? We will return false because we will have an invalid BST.
10:12But if our current root satisfies both conditions, what will we do?
10:17We will analyze both subtrees. We will analyze all the notes on the left subtrees and the right subtrees.
10:24If both return true, then we will return true.
10:28So we are going to return this valid BST for the left root.
10:34What will we do in the left of the root?
10:36What will we do in the left of the minimum?
10:37The minimum will be maximum.
10:41And, and, is valid BST for the right root.
10:48What will minimum value be below root?
10:52And what will we do in the left of the root?
10:53Which will be our current maximum, or infinity.
10:56So if both returns true, then it will be true.
10:59No, if any subtrees are invalid BST, then our whole BST will be invalid.
11:05So these are 4 conditions, which we can check whether our BST is a valid BST or not.
11:12Now, already we have created a tree, it was a valid BST.
11:15So we will print it.
11:16If it is valid BST.
11:19If it is valid BST, we will pass null nodes.
11:26So here we will print valid, else not valid.
11:34Let's do this save and run.
11:39If we create a tree here, which we can't create from here.
11:47But we change the conditions of insert.
11:48We change the conditions a little bit.
11:54Or, where the insert, we add 1, 1, 1.
12:03So what happened in this case?
12:051, 1, 1 is not valid BST.
12:07Because duplicates are not allowed.
12:08So it will be invalid BST for us.
12:11Let's try to dry run this again.
12:14Let's take two examples to dry run.
12:16One example is our valid BST.
12:19In this case, what will happen?
12:201 will be our minimum, which is null.
12:221 will be our maximum, which is null.
12:25Now, we will check the left subtree here.
12:27If the minimum value is null,
12:29and maximum value will be our 5.
12:313 will pass all the tests.
12:32What will the left subtree do?
12:34Minimum will be null.
12:36Maximum will be null.
12:38This will become a valid BST.
12:41We will return true.
12:43What will we do for 3?
12:44Now, we will call right.
12:45For right, minimum will come 3.
12:49Maximum will come 5.
12:51This will also be a valid BST.
12:53And we will return true.
12:54If it comes from both sides,
12:56then our 3 will return true.
12:59If it comes from left subtree,
13:01then we will call right subtree.
13:03Minimum will be 5.
13:05Maximum will be null.
13:07Minimum will satisfy that condition.
13:10If it comes from the left subtree.
13:10This is the 8 valid subtree.
13:12So, we will return true.
13:13On the right subtree,
13:14then 5 will return true.
13:16In this case,
13:17if we see an invalid case,
13:21what happens in this case ?
13:23Minimum will be null.
13:25Minimum will be null.
13:26The maximum will be null.
13:26So, we will go to the left subtree.
13:28Minimum will be null.
13:30Maximum will be 3.
13:32Then, in this left subtree.
13:34Minimum will be null.
13:36Maximum will be 2.
13:37So 1 will pass all the conditions and return to true
13:41And as we go to 2, we will go to 4
13:43So 4 will be minimum 2, maximum will be 3
13:47Now what will 4 do?
13:49It will not satisfy this condition
13:51So 4 will return to false
13:53Left sub tree came true and right sub tree came false
13:56So 2 will return to false because right sub tree is not valid BST
14:00Just as we have left sub tree, we have false return to false
14:04So 3 will return to false
14:07So this will become an invalid BST
14:11So this way we can try to dry run that our code will go
14:15So validate BST code, I hope you will understand it
14:19And the whole concept of BST is very useful when we talk about the placement of interviews
14:24Or if you will see some practical cases and you will go to Google yourself
14:29I hope BST is clear to us
14:32And we will practice this more and more
14:35To solve different questions
14:36So this is just one of the important locking cases
14:36When you touch off mod體
14:36To state relationships and舒展示
14:36And they will have to start each other
14:36If the actual condition will always follow
14:36And finally we engage
14:36meet new