• 12 hours ago
Transcript
00:00Next question which we are going to solve, it is called Minimum Distance Between Two Notes
00:04So I will give my two notes of my tree and I have to take out the minimum possible distance
00:10So here if I gave N1 to equal 4 and N2 to equal 6
00:15So the distance between them is the total number of edges which they put in the path
00:20So this distance will be equal to 2, this is equal to 1, this is equal to 2, this is
00:26equal to 3, this is equal to 4
00:28So the minimum possible distance will be equal to 4
00:32So if I take here, N1 is equal to 5 and N2 is equal to 3
00:38So the distance between them is equal to 1, 2 and 3
00:42So the answer is 3
00:44Now the minimum distance between two notes is the best way to take LCA
00:50Lowest common ancestor
00:51So if I have to do this question, I will do this with lowest common ancestor
00:56We will do this first with lowest common ancestor
01:00So what we will do, in a binary tree, within two notes
01:07Minimum Distance is the best way to take LCA
01:10Take out the distance from LCA and take out the distance from LCA
01:15And if we add the distance from LCA
01:17Then we will have total minimum distance
01:20номineres from LCA from LCA. So if you take up the distance between LCA and N2
01:23So let me remove this two notes from LCA
01:25So let me remove this in two notes from LCA
01:26I remove first
01:26Remove distance 1 from LCA, LCA
01:29So give up the distance from LCA, LCA from LCA from LCA
01:36To M One to K distance
01:36Then we remove distance 2 from LCA from LCA from LCA
01:46And LCA from LCA from LCA from LCA from LCA from LCA to LCA from LCA to N2
01:494K equal, N2 6K equal.
01:52What is LCA? LCA will be 1.
01:54Lowest common ancestor is 1.
01:56Now, the distance between 4 and 1 and 1 is 2.
01:591 and 6 is 2.
02:012 plus 2 is equal to 4.
02:03So, my answer will be minimum distance between N1 and N2 is equal to 4.
02:08So, I will simply remove LCA.
02:10Now, we have already asked for LCA to remove LCA.
02:13So, we can understand the code properly how to remove LCA.
02:15We will talk about how to remove LCA from the node to the distance.
02:21LCA, because my ancestor is my ancestor,
02:23I will try to make my parent or parent in my hierarchy.
02:28I will use LCA to make my LCA exist.
02:31Let's say that my LCA is 1.
02:34So, I will start with N1 first to search before N1.
02:39As I get N1, I will return the distance from here to 0.
02:45First, 1 will check in my left subtree if 2 is equal to N1 or not.
02:50So, 2 will check in my left subtree.
02:52If 4 is equal to N1?
02:54Here, the value of N1 will be equal to N1.
02:56So, what will I do?
02:57I will return my distance from here to here.
03:00That means, 4 and what distance between N1 and N1 is.
03:03This distance will be equal to 0 because the value of N1 is equal to N1.
03:07So, if both overlap, that means, 4 is N1.
03:10So, how much distance between N1 is equal to N1?
03:11How much distance between N1 is equal to N1?
03:13So, I will pass 2 to the distance as 0.
03:16Now, what will I see?
03:172 will see how much distance between N1 and N1 is.
03:20If the distance between N1 is 0 is 0,
03:23So, what will I say?
03:24How can I say that the distance between 2 is 0?
03:26That will be equal to 1.
03:28Because after 2 is if this purchase is what the distance is sollten.
03:31So, if the distance could river theboat?
03:323 between 1 is higher level.
03:34So, the distance will be equal to to 1.
03:352 will return here to 1.
03:38Then, 2 will return here from now .
03:39So, between 1 and 4
03:39In the distance, close as 4 by jå ´,
03:40greater distance can come?
03:42It will be equal to 1,
03:44With the distance?
03:44At first, the distance between 1 can come?
03:45And we will return at 2 to 1.
03:46So, I can say that this exchange between 1
03:48as work equals to 1 and which node N1,
03:50It may have given 2 between 9,
03:50einen kom�ter to 4
03:51between K1.
03:55The distance between 2 is equal to 2 shown under the distance.
03:57If we try to find out the distance from LCA
04:01N2 is my node
04:03First of all, we will go to the left subtree
04:05Is my left subtree not exist?
04:07So I will ask if 2 is equal to N2
04:10So I will ask if this is equal to N2
04:14Then it will go to our null
04:16As I reached the null, my base case will hit
04:19Now the value exists, we can return this to the null
04:23Minus 1 means
04:28Node doesn't exist
04:30So what will 4 do?
04:324 will return to our upper level
04:35Minus 1 means that I don't exist
04:38Minus 1 means that I don't exist
04:39Then 2 will get here
04:40Then 5 will return to the null
04:425 will return to the null
04:45So what will return to the null
04:472 will return to the null
04:48Because in this null
04:502 will not exist
04:52Minus 2 will not exist
04:54I will remove them for clarity
04:56Then what will 1 do?
04:571 will ask to the right subtree
04:593 will ask to the left subtree
05:023 will ask to the left subtree
05:046 will get forth
05:06That it will return to the null
05:19and in right we have to search our n. And in base case, this will happen that if my root
05:26is null or equal,
05:28in that case, I will return minus 1. This shows that my node doesn't exist here. I will check from
05:33left and right.
05:35If both have minus 1 minus 1, then I have to return minus 1. If both have a valid value,
05:42valid value means greater than minus 1 value, then I will return that value plus 1.
05:48Because I am a level above, if I am in the parent node, and I am in the sub tree,
05:53then I will get a distance between 1 and equal. This will be my pseudo code.
05:59Here I will get 6. What will I do? 6 will return 3 to 0.
06:05What will I do? 3 will already get 0. So, 3 will return 1.
06:11So, 1 will return 2.
06:16This means that the distance of 1 to 6 is equal to 2.
06:21So, the distance of 1 is 2, the distance of 2 is 2, and the answer will be 4.
06:26For this reason, we will write a pseudo code that is going to be time complexity.
06:30Linear time complexity.
06:31I will go to each node.
06:33First, compare it to each node.
06:34If my node exists on there, I will return 0.
06:38The distance is 0.
06:40No, then I will find the left and right.
06:42If both have returned minus 1, then I will not exist.
06:46I will also return minus 1.
06:50So, I will add 1 and that will be my root distance.
06:55And in the base case, if my root is null, I will return minus 1.
06:59It means that my node exists here.
07:02In the main function, I have already given a binary tree.
07:05I have already given a binary tree.
07:06I have constructed it.
07:06I have made the root.
07:07I have made the root.
07:08With the same, I have my n1 and n2.
07:10The value of n1 is equal to 4 and n2 is equal to 6.
07:13I am showing that the distance between them is equal to 4 and 4 edges.
07:18Now, for the distance to find out, I will call a function.
07:20I call public static int.
07:24I will name it minimum distance.
07:27In the minimum distance, I will have a parameter of my root node tree.
07:31One will be my node n1 and one will be my node n2.
07:35What will I do?
07:37I will find both LCA.
07:38Node LCA is equal to.
07:41I will call LCA2.
07:43This is my LCA function.
07:45Which we have already written LCA question.
07:47This is the same function.
07:48I will write this function again.
07:49If we are opening a new sheet, we are opening a new file.
07:53This is my LCA function.
07:55I will call LCA function.
07:57I will pass the root root n1 and n2.
08:02So, I have my lowest common ancestor.
08:04Now, I will create another helper function.
08:09I will name it LCA distance.
08:11That is, LCA distance from any node node.
08:14I will pass my root first n1.
08:17So, this will be my int distance 1.
08:22Then, my distance 2 will be my LCA distance.
08:27My LCA distance will be equal to root.
08:29In fact, root is not here.
08:30I will pass my LCA.
08:33This will be my lowest common ancestor.
08:37This will become my distance 2.
08:39And, I will return my distance 1 plus distance 2 value.
08:45Now, let's write the function.
08:47This means, we are going to complete our LCA distance.
08:51Public static int LCA distance.
08:55In which I have my root, I have my root.
08:59I have my root in this parameter.
09:01And, the other one will be my node n.
09:04With any root, how do we find out the distance?
09:08That's why we are going to write code.
09:09That's where I have to pass the root.
09:10Where I have to pass LCA.
09:11Where I have to pass N1 or N2.
09:14If my root is null.
09:16Then, there is no distance exist.
09:18So, I will return to here.
09:19So, I will return to here minus 1.
09:21No.
09:22Then, if my root data is equal to my n.
09:25I mean, my root is same.
09:27So, my distance will be 0.
09:29So, I will return to here 0.
09:30In other cases, I will try to calculate my left subtree and right subtree.
09:36So, my left distance will be equal to LCA distance.
09:41root ka left to n.
09:45And, right distance is equal to LCA distance for root ka right.
09:50And, my n.
09:54Now, if my two values are minus 1.
09:56Then, I will return to here minus 1.
09:58No.
09:58I will return to here.
09:59I will return to here.
10:00I will return to here.
10:03If my left distance is equal to minus 1.
10:07And, my right distance is also equal to minus 1.
10:11In this case, I will return to here minus 1.
10:16Or, what do I have to do?
10:18Or, I will check that if my left distance is equal to minus 1.
10:24Then, my height.
10:28And, my left distance is equal to thisoma.
10:35So, if my right distance is equal to thisoma 0.
10:36Saweisen is equal to thisoma.
10:38That's the sameoma.
10:53So, my left distance
10:54If we see in the sub-tree if we see exist, then remove the distance from the right
10:57sub-tree and remove the right sub-tree. If left and right
11:00both distance is minus 1, that means I have minus 1
11:03return to my sub-tree, the root node
11:07n does not exist. If left
11:09distance is minus 1, it means right distance
11:11valid value. So we return right distance plus 1
11:15If not, right distance is minus 1
11:18then left distance plus 1 returned. So this is
11:21my LCA distance. Minimum distance
11:22first LCA-N1 to the distance and LCA-N2 to the distance
11:26Both distances I will add and that will be my final
11:30minimum distance value. I will make the main function
11:32what is going to do in the main function? I will put a call
11:36minimum distance which I will pass
11:38root n1 as 4, n2 as 6
11:42Save and now run
11:45So my answer is 4 or equal
11:50which is my minimum distance
11:52which is my minimum distance. Here
11:53if I take 6 to the left, then I will add 5
11:55then I will add LCA-N2 to the right
11:58and minimum possible distance will be equal to 2
12:00This will also be run
12:03So my answer is 2 equal
12:05So in this way, we find out between 2 nodes
12:09in a binary tree