• 12 hours ago
Transcript
00:00Next question which we are going to talk about is its name is kth ancestor of a node.
00:04Kth ancestor means for any particular node, I want to get the kth ancestor of a node.
00:09Like this binary tree which I have given, I want to get the second ancestor of a node.
00:16Where node value is 5 equal and k value is 2 equal.
00:20For this node, its immediate parent is its first ancestor.
00:25So 2 will become my first ancestor.
00:27And the parent means 1, this will be my second ancestor.
00:33That's why I can say that if node value is 5 and k value is 2, then my answer will
00:37be 1 equal.
00:39Now the approach of this question is how to solve this question.
00:42The approach is something like that, first of all, I will start with root.
00:46I will search my node.
00:48First step would be to find my node.
00:51I will search my node.
01:09So I will return there to me to 0.
01:13What is 0?
01:150 basically my distance.
01:170 is root and node distance.
01:20And here I have 0 returned here.
01:22So what will receive the above level?
01:25If we think that I will search for 5.
01:27My node was 5 and k was 2.
01:29So if 5 will return 0, then 2 will receive 0.
01:33If 2 will return 0, then 2 will return 1.
01:34Then 2 will return 1.
01:35And then 1 will return 1 to equal 1.
01:39If 1 will see that I will add my distance.
01:41So my value is k equal.
01:43That means I will become a node's kth ancestor.
01:46If there is also a node's kth ancestor, then we will print that node's value.
01:51I will try to remove the minimum distance.
01:53I will try to remove this strategy.
01:55We will write code as well.
01:57So there will be some modifications.
01:59How will the modifications be?
02:00First, I will check what my root data is equal to my node.
02:04I have returned 0 to 0 if it is equal.
02:07This is my distance.
02:08I have to return distance every time.
02:09Because my k is also a distance.
02:12Which distance is the distance?
02:13My ancestor and my node's distance.
02:16Then what do I do?
02:18I will calculate my left distance.
02:20And I will calculate my right distance.
02:23Now, left distance and right distance.
02:25If both values are minus 1, then I will return to my minus 1.
02:29And if someone has given me a valid value,
02:33then I will check that distance plus 1.
02:38If this value is k equal to k,
02:41then I will return to k equal to k.
02:44So I will print my root data.
02:49What will the logic be?
02:50The logic is that I will check what my value is.
02:55If it is equal to my node,
03:09I will return to my root data.
03:12And I will return the value to this node.
03:17So I will add 1 to this node.
03:19If I have added 1 to this node,
03:22then I will add 2 values.
03:23If the value is k equal to k,
03:25then I will print my root data.
03:27Then I will write code to this node.
03:32I will write code to this node.
03:33I have a main function.
03:33I have a tree given.
03:34And I have my nodes given.
03:37And my n value is 5 to equal.
03:39And my k value is 2 to equal.
03:41And my answer should be 1.
03:42I have a main function.
03:44Public.
03:45Static.
03:46I will put it in return type.
03:47Because I want to track distance.
03:49So I will return distance.
03:51The name is k ancestor.
03:54In k ancestor,
03:55then I will add root.
03:57And I will add node value n.
04:00And I will add distance k.
04:02So the logic is this.
04:04If my root data is equal to n,
04:07means my root data and n overlap.
04:09So the distance should be equal to 0.
04:11So I will return the value to 0.
04:14No.
04:14So I will calculate my left distance.
04:17Which will be k ancestor.
04:19I will pass the root of n and k.
04:22Besides, I will calculate the right distance.
04:25Right distance will be k ancestor,
04:28root of n and k.
04:30Now, like left distance and right distance.
04:33I will calculate the right distance.
04:34So I will check the two minus 1.
04:37Minus 1 means that the node will find out.
04:41I will not be ancestor.
04:43If I am not ancestor,
04:44it will not exist in my sub tree.
04:46It will not exist in my sub tree.
04:47So there will be minus 1 return.
04:50So if left distance is equal to minus 1.
04:53And my right distance is equal to minus 1.
04:56In that case,
04:57I will return the value minus 1.
05:00This means that the node exists here.
05:02So I will not tell the distance here.
05:04I will not tell the distance.
05:06So I will take the maximum.
05:08If I take the maximum.
05:08If I take the maximum.
05:08If I take the valid value.
05:09Because it will be minus 1.
05:11It will be 0.
05:11It will be 1.
05:12So it will be maximum.
05:14So int maximum is equal to.
05:18Math dot.
05:19Max of my left distance.
05:22And my.
05:23Right distance.
05:24And my.
05:30And my.
05:32And my.
05:46Which is going to be equal to.
05:48Maximum plus 1.
05:49This will be my.
05:50My.
05:51Distance.
06:01This will be my value.
06:13This will be my value.
06:50Valid distance.
06:51If I take the first.
06:53Ancestor.
06:55Ancestor.
07:21Ancestor.
07:21I will go to the segunda tree.
07:22Then there will also be 2.
07:23Left distance can be to theiéns.
07:26to the third tree.
07:26The four archetype puts 4.
07:29We turn to the left tree.
07:30So here is minus 1.
07:33And the right tree too.
07:34To the right tree is minus 1.
07:35To the 4 which I will return.
07:364 will return minus 1.
07:38The из-して minus 1.
07:39You mean the node of next tree.
07:40Doo SND for 4 of the.
07:42Not the MARTIN's.
07:43Sometime MAN.
07:43So there will not exist 2.
07:47There will not have an encestor.
07:48As for the okay,
07:49Left on that tree.
07:50Now check the right subtree in the right subtree
07:52In the right subtree there is 5
07:55This condition will be true
07:56That the root data is equal to my node
07:59As it is true, 5 will return
08:015 will return value to 0
08:042 will return value to 0
08:052 will return value to 0
08:072 will take maximum
08:08What will happen
08:10Maximum will be 0
08:11and minus 1
08:12which will be 0
08:13So 2 will see
08:14I will add 1
08:16So it will be equal to 0
08:18So 1 will not be equal to 1
08:20Because 1 is not equal to 2
08:22So what will return value to 0
08:24So 0 will return value to 0
08:27So 1 will return value to 0
08:28Left 1 will come to 1
08:29And what will happen to 1
08:31What will happen to 1
08:32Because all nodes will return value to 0
08:35Because we will get the end
08:37Where will not get the end
08:37So 1 will be left
08:40If 1 will come to 1
08:42So 1 will come to 1
08:441 and minus 1
08:45maximum will turn to 1
08:46Which will be 1
08:47And that will return value to 1
08:49Is this equal to k
08:50Its value will become 2
08:51And k be 2
08:52So these both will be same
08:54This is why line
08:55our answer will be printed
08:56And according to our root
08:56MinINGS
08:57Ou 1
09:00Finally we necessarily return
09:00Which means 1
09:021 plus 1
09:02equal to 2.
09:04So,
09:04in this way,
09:05we have our
09:05KATE ancestor
09:06in any particular node.