- 12 hours ago
Transcript
00:00Next question, which we will talk about is lowest common ancestor. Ancestor means poor-wage, in English. Now, ancestor, which
00:09is property, we have already studied in the introduction of trees for a node.
00:14So, which are the ancestors of the node, which are the parent, or the parent, which are the root of
00:22the node, which are all the ancestors of this particular node, including the root itself.
00:31So, if I talk about 4 here, then the ancestors of 4 will be 2 and 1. If I talk
00:36about 3, then the ancestors of 3 will be 1.
00:38If I talk about 5, then the ancestors of 5 will be 2 and 1. So, what do I do?
00:44I will give two nodes.
00:46One node will be N1 and one node will be N2. I will give them the lowest common ancestor for
00:53both nodes.
00:53Common ancestor, which is the value of both ancestors, is common. Common node, which we are talking about.
00:58And lowest possible, which is less than less possible. If I have given 4 and 6, lowest common ancestor.
01:06So, 4 of 1 ancestor is 2. 6 of 1 ancestor is 3. Now, in 2 above 1 ancestor, 4
01:14is 1 and 6 is 1.
01:17So, their first common ancestor is 1. Lowest common ancestor is 1.
01:23So, the answer will be 1. The lowest common ancestor is 1.
01:28So, we call it LCA. My LCA will be equal to 1. If I have N1 is equal to 4
01:35and N2 is equal to 5.
01:37So, the ancestors of 4 will be 2 and 1. 5 will be 2 and 1. So, what will their
01:43lowest common ancestor?
01:46So, the lowest common ancestor, what will happen in N2. If you have N1 know what the time is 2
01:58tulee will be 2 but the most common ancestor.
02:14I have my LCA. So, LCA, which is our approach, let's say that I have N1, 4K equal, N2, 6K
02:23equal. This is my N1 and this is my N2. Logically, we have to take this node to the root
02:32and this node to the root.
02:35So, what do I do? I have to take this path. First, I have to take this path 1, which
02:40is my N1 path. I have to store all nodes in which I have to store all nodes.
02:45Which we have to take the path, as we go to the root and go to the root. I start
02:52with the root.
02:53First, I have to take the node number 1, then I have to take the node number 2 and then
02:58I have to take the node number 4.
03:00So, this has been my path. The path is where to where? I have to take the root of my
03:05node.
03:07I have to store this path. One, two, four.
03:11Second, I have to calculate my path number 2, which is my N2 path.
03:15For the path, I have to create an array list again.
03:18If I can create an array list again, I can create an array list again.
03:21And this path will go to N2.
03:23First, I will go to 1, then I will go to 3 and then I will go to 6.
03:29So, when I have to take the path to N1 path, root from node.
03:32And then I have to take the path to N2 path, root from node.
03:35So, what can I do? I can traverse these paths.
03:37I can start to traverse this path.
03:39Now, as I have to take these paths, which is my last common node.
03:43That is my LCA.
03:45I mean, I have to take two pointers.
03:47One is here and one is here.
03:48I will see them.
03:49What is the value of 1 and 1?
03:52Yes, it is common.
03:53So, 1 is 1 is 1 and 1 is ancestor.
03:551 is 1 and 2 is ancestor.
03:56So, 1 is a common ancestor.
03:57Now, after 1, I will see.
03:59After 1, there is 2 and 3.
04:01This is not common.
04:02So, the last common ancestor was 1.
04:06So, my LCA will become 1 equal.
04:10In another example, we can try to run this.
04:13Let's take this time, N1 and N2 values are different.
04:17This time, N1 is 4 and N2 are 5.
04:20So, first of all, I will calculate path 1 for N1.
04:24N1, the path 1 will be equal to, first of all, 1 store.
04:29Then, 2 store.
04:31Then, 4 store.
04:33In the same way, if I calculate path 2 for N2.
04:37Then, I will also have some value store.
04:40First of all, I will also have 1.
04:42Then, I will also have 2.
04:43Then, I will also store 5.
04:46So, I will start with 2 pointers.
04:48Here and here.
04:491 and 1 compare.
04:50Do both same?
04:51Same.
04:52So, I will also add 1 and 1.
04:53I will compare it with this.
04:54I will compare it with this.
04:552 and 2 compare.
04:56Do both same?
04:57I mean, both same.
04:58Same means that 1 is N1 and N2 are both ancestor.
05:02And 2 is N1 and N2 are both ancestor.
05:05Because 1 also comes in this path.
05:07And 1 comes in this path.
05:092 comes in this path.
05:11And 2 comes in this path.
05:13Then, I will check 1 and 1 go to 4 and 5.
05:175.
05:17Is 4 and 5 same?
05:194 and 5 same.
05:204 and 5 is not the same.
05:214 and 5 is different.
05:21Meaning, the last common value was my 2.
05:25So, what will my LCA become?
05:27LCA become 2 equal.
05:29So, what will I do?
05:30I will do every time with my root.
05:32I will calculate the path to the node.
05:35This will be my first step.
05:36Second step is,
05:38that a loop is running a loop.
05:39And then,
05:40the last common node is left.
05:42Which will be my last common node.
05:44That will become my LCA equal.
05:47First step,
05:49we will discuss a little detail in detail.
05:51First step,
05:51What will happen?
05:52First step is,
05:53that you will take root to the path.
05:57I will create a function.
05:59Traversal name.
06:00Or,
06:01I will create getPath.
06:03If I pass the root,
06:05I will pass the root.
06:06I will pass my node with my other.
06:08And I have passed an array list.
06:10Path type.
06:11I will store my path.
06:12First,
06:13my path is called ArrayList.
06:16The root will be stored.
06:18One will be stored.
06:18Then,
06:18I will come to 2.
06:19Then,
06:19I will store 2.
06:20Then,
06:21I will store 4.
06:22So,
06:23I will store my notes.
06:25Let me take my path.
06:28For 5.
06:29For 5,
06:31I will go to this root.
06:32I will store 1.
06:33Then,
06:33I will store 2.
06:34Then,
06:34I will go to 2.
06:35Then,
06:35I will store 4.
06:38Then,
06:38I will store 4.
06:39What will happen?
06:40Then,
06:40I will call your left subtree.
06:42Null.
06:42Null will say,
06:43that the node exists here.
06:44This will return false.
06:46Then,
06:46the right subtree will call.
06:48This will also say,
06:48that the node exists here.
06:50This will also return false.
06:51So,
06:524 will return false.
06:53This will also return false.
06:53That means,
06:534 or 4.
06:55No,
06:56No,
06:57No,
06:57No,
06:57No,
06:57No,
06:57No,
06:58No,
07:05No,
07:06No,
07:06No,
07:06No,
07:07No,
07:09No,
07:10No,
07:10from that
07:11sub tree is false. Now he will ask from right sub tree. In right sub tree I will call 5
07:17and I will write 5 in the path
07:19and with this 5 will return true. Now 2 has false here and here true. True also comes from one
07:26side, means 2 exists in the path
07:28If you exist in the child, you will go to 2 or true. So we will be intact in area
07:33list. Then 2 will return 1 to true.
07:36You will know that in left sub tree my node exists, so 1 will be in the path. So by
07:42taking root to the path
07:44my ancestors plus my node itself, I will add in an array list. If I want to dry run, I
07:54will find three ancestors
07:57What will I do? First of all, I will add 1 to 1. 1 will ask 2, 2 will add
08:032, 4 will ask 4, 4 will ask 4
08:06from left sub tree. Left sub tree says, I don't have node in left sub tree, so it will return
08:11false.
08:124 will return false. 4 will have false and then right sub tree will also be false.
08:16Then 4 will know that left sub tree and right sub tree do not exist. That means I can't
08:20have path part. So 4 will do here, remove. Then 4 will return false. Then 4 will return false.
08:28Then 2 will ask 5. 5 will add and then left sub tree will return false. Right sub tree will
08:33also
08:33false return, so 5 will also be removed from my path. Then 5 will also return 5 to my path.
08:39And then 2 will also be false. So left sub tree also be false and right sub tree
08:44means that whatever node is, that means 2 doesn't exist in this sub tree. So 2 will also
08:49point 2 will not be part part. So 2 will return 1 to 1 to false. So here, 2 will
08:55return 1 to 1 to false.
08:56I will remove it for clarity and as 2 has returned false, so 2 will be removed from my path.
09:03Now what will 1 do? If you ask the right subtree, as you ask the right subtree, you will add
09:073.
09:07You will add 3 and you will get the node from here, so we will do return true.
09:14So 1 will be intact, 3 will be intact and this will be stored by 3.
09:20So in this way we will store our path.
09:23As I have stored the path, then simply add a loop.
09:27In the loop, I will have a pointer that will increase and increase.
09:30And what will I do?
09:32In my second step, I will run a loop.
09:37From i is equal to 0 to i, and path 2 is not small.
09:46I mean, if you want to traverse these two loops, then i should be small.
09:51And I will find out my last common ancestor.
09:57Last common ancestor will be my lowest common ancestor in these parts.
10:04For this, we are going to write code.
10:06In the main function, I have given my tree.
10:09And the value of n1 is equal to 4 and n2 is equal to 5.
10:12So the common ancestor of these two, I will know that I will have 2.
10:16I am going to write the function to find out the common ancestor.
10:19Public, static.
10:21The return type will be void.
10:23Or we can give this node.
10:25The name is LC. Lowest common ancestor.
10:27For this one, I will have a root.
10:30This root of my tree and my tree, n1 and n2 is given.
10:36So for these two nodes, I will find out the lowest common ancestor in this root.
10:40So first, I will find out the path.
10:43I will make an array list of integer.
10:50Path1 is equal to new array list.
10:54And above, one other array list of integer.
10:58Path2 is equal to new array list.
11:01I will make a function called getPath.
11:03In which I will pass.
11:05I will make my root, my n1 and my path1.
11:09So what will I do?
11:11I will take root to n1.
11:12I will store the path to n1.
11:15And getPath to call for my root, my n2 and my path2.
11:20So what will I do?
11:21With root to n2, I will store the path2 array list.
11:25So when I have a path, I will make last common ancestor.
11:31I will calculate the last common ancestor.
11:35I will take a pointer.
11:37I is equal to 0.
11:38This will start with 0.
11:40In this case, I will increase the path1.length.
11:44I will increase the path1.length.
11:45And path2.length.
11:48I should not have a size.
11:49Because I have an array list.
11:51So if I will increase the path2.length.
11:56And the path2.length.
12:01If I will increase the size of my path2.length.
12:08So what do I do in that case? I will do a break from loop.
12:12So this loop will continue until my value is equal to path 1 and path 2.
12:17The values are the same from the i-th index.
12:21And as it is unequal, I will do a break.
12:25And the last equal value will be equal to the i-th index.
12:34I-1-th index, why? Because this loop will break,
12:37the value of i will be unequal nodes.
12:41As there are equal nodes, my pointer will be i-1.
12:45So my node lca will be equal to path1.get i-1.
12:53So the value of i-1-th value will become my lca.
12:58We will make this getPath function.
13:02So public static void getPath.
13:06This return type is in fact boolean.
13:09We will know from boolean that which node we are trying to store the path,
13:14that node actually exists in which sub-tree.
13:17So in getPath I have a parameter, my root.
13:20And with this, my node n and my array list of integer my path.
13:33Also here, the array list of path,
13:36I will store the integer node.
13:38I can store the entire node.
13:40So we will change our path to node type.
13:44So we will store all nodes.
13:47Here it will be i-th path2.size.
13:53Now what are we going to do in getPath?
13:54What are we going to do in getPath?
13:55First of all,
13:56I will add my node in my path.
13:59So path.add my root.
14:02Then I will check that if the root data is equal to n.
14:07If the root data is equal to n,
14:09I mean that I have my node.
14:11So I can return directly to true.
14:14If the node gets the same,
14:15I need to go to the sub-trees.
14:16I can return directly to true.
14:18return to true.
14:19And if the node doesn't get the same,
14:21then we will go to the sub-trees and search.
14:23Then I will add call to getPath to for root.
14:27Left.
14:28Then I will pass the end and path.
14:32Here,
14:34I have a boolean value return.
14:38Found.
14:40Left.
14:41Then I will call to right.
14:43Then I will return there.
14:45Found right.
14:49Is equal to getPath.
14:53For my root.
14:55Right.
14:56So I have to call to my left-sub-trees.
14:58That if I get the boolean value or not,
15:00I will get the boolean value.
15:01And in the right-sub-trees,
15:02I will get the boolean value.
15:04Now I will write the condition that
15:05if my node is either in my left-sub-trees,
15:11or in my right-sub-trees,
15:13or in my right-sub-trees
15:16or in my right-sub-trees.
15:17But if my node is either in my left-sub-trees,
15:20or in my right-sub-trees,
15:22or in my right-sub-trees.
15:22In that case,
15:23my root is not my path.
15:25So I will remove it.
15:27So I will remove it.
15:43If my root exists,
15:43it is my current root.
15:45I will remove this root.
15:46I will remove this root.
15:47Because I will not have path.
15:49Because in my right-sub-trees,
15:50or in my right-sub-trees,
15:52I will not do node-n exist.
15:55And this case will also be
15:56that if I will return my root
15:58to a null-to-equal.
16:00So I will return the false.
16:02Here I will not do node-n exist.
16:06And here, in last,
16:07I will return the false.
16:09If the sub-trees exists,
16:11then I will return the true return
16:12or not,
16:12then I will return the false value.
16:15Now, in LCA,
16:16what do I have to do?
16:17I have to calculate both paths
16:19with the get-path help.
16:21And then,
16:21I have left the last common ancestor.
16:23Now, I will return the lowest common ancestor.
16:28Let me summarize this.
16:30I have made a LCA name function
16:32which I have removed from root
16:35and removed from root
16:36and removed from root.
16:37How did it start?
16:39I have started that
16:40that I will return the path to root
16:40to some node-n.
16:43So I have taken an array list
16:44called path.
16:45First of all,
16:46I have added the path.
16:47After that,
16:48if the root data
16:49is equal to my value
16:51to my root value,
16:51then I will return my data
16:53directly to true.
16:55That means,
16:55the data is found.
16:56No,
16:57I will find data in my left-sub-trees
16:58and right-sub-trees
17:00and try to calculate the path
17:01from there.
17:02If the left-sub-trees
17:03or the right-sub-trees
17:04got it,
17:05then I will return the true.
17:06If it doesn't get it,
17:07then my current root
17:09is not part of the path.
17:10In that case,
17:11I have removed the current root
17:13which is at my last index
17:15and I will return the false
17:17here.
17:17Now,
17:17as I have both paths
17:18I will run a loop
17:19with i is equal to 0
17:21and I will go to the array list
17:23to the sizes of both
17:24and as I have
17:26un-equal values
17:27and as I will break
17:28this loop
17:29and what will be
17:30last equal value?
17:31If i is un-equal
17:32then i-1
17:33will be last equal value
17:34which i have returned
17:35as my LCA
17:37Now,
17:38for this node-4
17:39and this node-5
17:40I am going to remove
17:40the LCA
17:41which should be 2
17:42equal
17:42I will call
17:45LCA
17:45for my root
17:47n-1
17:48and n-2
17:48Let's save
17:49and run
17:54So,
17:55I have a node
17:55returned
17:56so,
17:56its address
17:58has been printed
17:59so,
17:59I need to print
18:00the node
18:00to save
18:03and print
18:032
18:04so,
18:052
18:05is the lowest common ancestor
18:07but,
18:07if I take 4
18:08and 7
18:09then,
18:10the lowest common ancestor
18:11is equal
18:12then,
18:14let's save
18:15then,
18:16the LCA
18:16is equal
18:18Now,
18:19this is my
18:21linear time complexity
18:22process
18:23This is my
18:23linear time complexity
18:25process
18:25because
18:26path
18:27calculate
18:27linear time
18:28will be
18:28linear time
18:29this will go
18:30every node
18:31this is the recursive function
18:33so,
18:34once
18:34end time
18:35will be
18:36bigger of n
18:37this call
18:39and this call
18:39will be bigger of n
18:40then,
18:41this loop
18:42will be bigger of n
18:43time
18:43so,
18:443 times n
18:45is equal to
18:46big of n
18:47so,
18:47linear time
18:48I have found out
18:48in my lowest common ancestor
18:49in linear time
18:50so,
18:53space complexity
18:54we are using
18:55two different
18:56analysts
18:57which,
18:57space complexity
18:59is also linear
18:59but,
19:00a different approach
19:01exists
19:01without any extra space
19:04and in linear time
19:06we can find out
19:07in single pass
19:08our lowest common ancestor
19:09find out
19:10we can find out
19:11two
19:11two
19:11three