- 12 hours ago
Transcript
00:00So in approach 2, if I find N1 is equal to 4K and N2 is equal to 6K, lowest common
00:06ancestor
00:06find out, if I want to say LCA, it is the first parent, which in the sub-tree my N1
00:17and N2
00:17both exist. What does this mean? If I want to say LCA for 4 and 6, then I want to
00:24say
00:29Ancestor, this is the first ancestor or the first node, which both N1 and N2 exist.
00:38So 4 and 6, 2K sub-tree both exist, and 3K sub-tree both exist, but 1 first node is
00:47in which the sub-tree, 4 exists here and the right sub-tree, 6 exists here. So here,
00:551 will become its lowest common ancestor. Now, let's say that I have 4 and 5 node.
01:01So what will I do? I will try to see 1 in the middle of 1 and 2 nodes exist.
01:09So here,
01:102 nodes exist in 2 nodes exist. Then I will try to see 2 for 2, if 2 nodes exist
01:15in 2,
01:16left sub-tree, right sub-tree, both nodes exist? Yes, here, 4 exists in 4 and right sub-tree
01:235 exists in 4. Then I will try to see 4, if 2 nodes exist here, then I will try
01:29to see
01:32first time that my nodes exist in a sub-tree, it will be a 2 node, so 4 and 5
01:38will be my node
01:41number 2.
01:42So what will I do first, I will apply the approach in which we will find out that node
01:48in which I exist in a sub-tree, we will write code in this approach and then we will dry
01:56run the code.
01:57In the code, I already have my main function, I have n1 and n2 values, now I have a new
02:03function called public static, its return type we will put node in the name, lca2
02:09equal.
02:10In the parameters, I have root, with my n1 and my n2, what will I do first, if the root
02:19data is n1 equal, or root data is n2 equal, then what will I do in this case, what will
02:27happen?
02:27In this case, my LCA, I will return my root here, meaning if I have given this tree in which
02:34the root is n1 and root is n2, or there will be any equal between those two, meaning I have
02:40as n1 passed, so in this case, LCA will be root here, so directly that node will return as
02:46my LCA.
02:47Then, what will I do in this case?
02:49I will try to find the left sub-tree, and try to find the LCA and right sub-tree in
02:54the
02:54LCA.
02:56So, I will call LCA2 to the left sub-tree, which I will pass the root to the left, so
03:03here
03:03I will return a node type to the left LCA, which I will store the value, then I will try
03:11to
03:13LCA.
03:14So, LCA2 to the left sub-tree, I will call for root to the right, so in the left sub
03:20-tree
03:20to the left sub-tree, my LCA left sub-tree doesn't exist, or in the right sub-tree doesn't exist.
03:26Now, let's assume a case, let's assume that I have left LCA's valid value, but my right
03:32LCA, that becomes equal to null.
03:34If there is a value valid here, I have left to the right to null.
03:39That means, if I have right LCA's value, I have null to be equal to null, then I have
03:46directly left LCA to return.
03:49This case is which case will be?
03:51Let's assume that I have N1 value is 4 and N2 value is equal to 5.
03:56So, in this sub-tree, what will I return to this left sub-tree?
03:59I will give you what I will return to this 4 and 5, the common ancestor will find out
04:03and I will return to this 2.
04:05For the right sub-tree, the right sub-tree will return null value to the null value.
04:09Or, if I have a valid value to the null value, I will return to this one.
04:11So, if I have a node, I have 1, and from left, I have a valid value to the right,
04:17and from right, I have null to the right side, this means that right side
04:20either N1 exists or N2 exists.
04:24So, if I have N1 exists or N2 exists, then 1 can always be the lowest common ancestor?
04:30No.
04:31Because if I have a valid value to the left, it means that N1 and N2 are left.
04:35And if N1 and N2 are left, it means that the lowest common ancestor will exist in the left.
04:41So, in this case, if I have a valid value to the left sub-tree,
04:49if I have a valid value to the right sub-tree, it will null to the right sub-tree.
04:52So, in this case, the left sub-tree has a valid value to the left sub-tree.
04:55It means that I have a valid value to the right sub-tree.
04:57That will be my answer and I will return to it.
04:59This is the same in the opposite case.
05:02If I have N1's value is 7 and N2's value is 6,
05:06In this case, what happens if I have a valid value to the right side?
05:09What value will be equal to the right side?
05:10And I will have null to the left side.
05:13This means that 1 cannot be my LCA.
05:16If I have N1 and N2 both exist in the right side,
05:18my LCA will also exist in the right sub-tree.
05:21How will it exist?
05:22In the right sub-tree,
05:24the right-sub-tree from the right sub-tree
05:24all answered on the right sub-tree
05:26that I will return directly.
05:27So, who will return to 1?
05:28What will return to 1?
05:296 to 3?
05:30First, as the LCA is a valid value to the left sub-tree.
05:34So, if I have a valid value to the right sub-tree,
05:35then I will return to the left value.
05:36And if my left LCA's value is a value of null to equal,
05:40So in that case, I will return my right LCA, so here I have found out if my common LCA
05:50exists
05:51But if there is no null between both, this is the case where the right null was, this is the
05:56case where the left null was
05:57But if there is no null between both, what do I have to do in that case?
06:01In that case, I mean in one sub tree my N1 exists, in one sub tree my N2 exists, I
06:06mean I am the first common ancestor
06:09In that case, I will return my value as my LCA
06:14This case will be the case if my root is null or equal to my null
06:18In that case, I will return my value as my null
06:21In that case, I can also combine these three cases
06:25Here I will write directly that if root equals null
06:31Then I will return my root as my null
06:34So I will return my single base case
06:36So if my root is null or the data is equal to N1 or N2
06:41Then I will return my root as LCA
06:43In that case, I will try to calculate LCA in the left side or left sub tree in LCA
06:46And in the right sub tree in LCA
06:48I will try to calculate LCA in both
06:50In that case, one is null
06:51So in that case, the other sub tree has the right value of LCA
06:54So I will return my root as well
06:55And both are not null
06:57In that case, there are valid values
06:59That means I will return my root
07:01Let's do this
07:02Save
07:03I will return my root for 4 and 7
07:04For the lowest common ancestor
07:08That should be equal to 1
07:09Let's do this
07:10Save and now
07:14Run
07:14Answer
07:15Answer
07:15I will return my root for 1
07:16Now I will return my root for 4 and 6
07:18To choose
07:19So I will return my root for 1 and 6
07:23To choose
07:25To choose
07:25To choose
07:26Now let's try to dry run and see it, let's say I have this tree given in which n1 value
07:31is 4 and n2 value is 6 equal
07:33I want to remove this node and this node to LCA
07:37What will I do? First of all according to the code, I will go to my root
07:41What will I do? First I will check the 3 conditions
07:45First is that the root is null
07:47Second is that the root is equal
07:50Third is that the root is equal
07:53In these 3 cases, we return our root
07:58So here, no root is null, no n1 is equal or no n2 is equal
08:02If there is no case in these 3 cases, we will try to find out our left sub tree
08:09So in the left sub tree, my job is to do my left LCA find out
08:15And my right LCA find out
08:18So I will go to 2, 2 will check if 2 is null, n1 is equal or n2 is equal
08:23or no condition is true
08:24So 2 will also find out our left LCA find out
08:27Then we will go to 4
08:284 will find out whether it is null or n1 is equal or n2 is equal
08:324 is equal or n1 is equal or n1 is equal or n1 is equal to n1
08:35So here, this condition is true or we will return our root
08:37So root, to return our root
08:39Here the value is equal to here
08:42That value will return the value of our node
08:45Now, 2 will return the value of left LCA
08:49This value is equal to 4
08:51Now 2 will right LCA value
08:53So 5 will be
08:54If 5 will be in the condition
08:56So 5 will be left LCA
08:58Here will return null
09:00Here will return null
09:02Now 5 will left LCA
09:05Right LCA null
09:07So conditions are
09:08That if right LCA
09:11Null is null
09:13So we return left LCA
09:15And if left LCA
09:17Null is null
09:20So in this case
09:21We return our
09:23Right LCA
09:25And if right and left
09:29Both not null
09:30So in this case
09:32We return our
09:33Root
09:34So in this case we have code
09:36So 5 will be left LCA and right LCA
09:39So first right condition
09:41If right LCA null
09:43So left LCA return
09:44So here will be null return
09:46So here will be
09:48Return
09:50Now
09:51To analyze
09:52To analyze
09:53To
09:53To
09:53To
09:54To
09:54To
09:55To
09:56To
09:56To
09:56To
09:57To
10:02To
10:03To
10:04To
10:04To
10:04To
10:04To
10:04To
10:04To
10:04To
10:07To
10:09To
10:10To
10:10To
10:15To
10:15To
10:15To
10:17To
10:19To
10:19call लगाएगा तो 3 पर आ जाएगा 3 call लगाएगगा अपने left elseya को call
10:23left elseya क्या return करेगा
10:25यह return करेगा इसे null value
10:27अब three call लगादेगा 6 को
10:296 पर जैसे ही हम आ जाएगा 6
10:31discuss की value equal हो जाएगी मेरे
10:33n2 के तो 6 n2 के equal हो
10:35गया तो return कर देगा खुदी को यानि six
10:37So, we return to node 6. Now, 3 has left LCA null, and right LCA is 6.
10:44When the left LCA null is gone, we return to node 6.
10:47So, 3 will return to node 6. So, 1 has left LCA, and right LCA is 6.
10:57So, 1 has left LCA and right LCA is not null.
11:00When both values are not null, we return to node 6.
11:09So, 4 and 6, which is the lowest common ancestor, that is equal to 1.
11:15And in some way, our code will work.
11:17Now, the special thing here is that we have not utilized any extra space in terms of an array list,
11:22any extra array list, any extra data structure.
11:25We have not used single pass.
11:27In every node, we have gone on every node, and we have gone on every node,
11:31we have calculated LCA.
11:33So, this is in linear time.
11:34We have solved with constant space.
11:37But since we are using recursion here,
11:40I have not included the recursion stack of the space here.
11:43So, if we include recursion stack of the space,
11:45then I will have linear space here.
11:48But, here, no auxiliary data structure has not been used in the use.
11:51Only recursion stack has been used in the use of recursion stack.
11:53So, there you go.
11:53There you go.
11:53And you want to do the string.