1 00:00:01,350 --> 00:00:02,070 Hi, everyone. 2 00:00:02,100 --> 00:00:07,890 So in this video, we will be writing the code for this problem and let's start writing the code so 3 00:00:07,890 --> 00:00:08,700 we discuss that. 4 00:00:09,510 --> 00:00:10,260 What do we need? 5 00:00:11,290 --> 00:00:16,340 We are going to write one helper function and what this helper function will do, it will do in order 6 00:00:16,350 --> 00:00:19,500 traversal so fording in order traversal. 7 00:00:19,650 --> 00:00:22,440 We obviously need that riyaz input. 8 00:00:22,860 --> 00:00:24,310 So we will take triazine. 9 00:00:24,330 --> 00:00:31,410 But we discussed that we need one variable count and we need one more variable answer. 10 00:00:34,840 --> 00:00:42,490 And how I will call this function, so initially the value of count to zero, I have not visited any 11 00:00:42,490 --> 00:00:46,290 of the nauseate and initially my answer is, let's say minus one. 12 00:00:47,710 --> 00:00:51,030 Then what I'm going to do, I will call this function in order. 13 00:00:51,730 --> 00:00:58,450 It will take root as input, it will take count as input, it will take answer as input, and it will 14 00:00:58,450 --> 00:00:59,920 take Ghias input. 15 00:00:59,920 --> 00:01:04,209 Obviously, we need to take as input also. 16 00:01:05,319 --> 00:01:09,480 And what this function will do, this function will update the value of answer. 17 00:01:09,970 --> 00:01:16,070 And when you have the value of answer, you can return the answer and the function is in order. 18 00:01:16,090 --> 00:01:18,660 So what it will do, it will perform the traversal. 19 00:01:19,300 --> 00:01:23,160 So for writing the code for another trevathan, it's very simple. 20 00:01:23,170 --> 00:01:30,190 First of all, base case, if your tree does not exist, you will simply on now in artemisinins left-to-right. 21 00:01:30,310 --> 00:01:36,910 So I will call in order and in order on the left. 22 00:01:37,800 --> 00:01:42,390 So count will remain same on several domain same and gavelled remains same. 23 00:01:43,390 --> 00:01:45,810 And now I am visiting the current node. 24 00:01:46,690 --> 00:01:52,440 So you remember the traversal is you are left then data and then right. 25 00:01:52,690 --> 00:01:57,010 So when you are visiting the current node, when you are visiting this node, then you will look on 26 00:01:57,010 --> 00:01:57,640 placeless. 27 00:01:58,270 --> 00:02:00,480 This will be the first node that we will visit. 28 00:02:01,810 --> 00:02:03,100 So let's do that. 29 00:02:03,250 --> 00:02:10,360 So what I will do here is I will do I am visiting the current node, so I will look after this place 30 00:02:10,360 --> 00:02:12,580 and now we need to check. 31 00:02:12,610 --> 00:02:17,590 So if the value of count become a clear Stookey, that means this is our answer. 32 00:02:17,620 --> 00:02:18,490 This is our node. 33 00:02:19,120 --> 00:02:21,910 So if this is our node, what will be my answer? 34 00:02:21,940 --> 00:02:27,090 My answer will be the data, which is route root value and stop. 35 00:02:27,100 --> 00:02:28,140 So we need to stop. 36 00:02:28,150 --> 00:02:28,960 So we will return. 37 00:02:32,400 --> 00:02:38,550 And if this is not the case, then obviously Left-to-right so we'll call the order function on the right 38 00:02:38,550 --> 00:02:45,600 subtree so rude, right, then government will remain same on several domains and cable means him and 39 00:02:45,600 --> 00:02:47,870 that that will be the complete code. 40 00:02:48,240 --> 00:02:49,740 So let's add in our code. 41 00:02:50,250 --> 00:02:52,590 Yeah, our code is working and let's it. 42 00:02:57,650 --> 00:03:02,610 So basically, our court is working and the court is so simple, right? 43 00:03:02,660 --> 00:03:05,370 What they are doing is simple for doing a traversal. 44 00:03:05,390 --> 00:03:10,810 The best case then left, the Spartans left, then this party's data. 45 00:03:10,820 --> 00:03:14,480 You are visiting the current north and this party's right subtree. 46 00:03:15,590 --> 00:03:16,370 Very simple. 47 00:03:17,120 --> 00:03:20,330 As soon as your visiting minority, you are doing Karplus plus. 48 00:03:20,750 --> 00:03:23,800 And since they are, Trevathan is basically sorted. 49 00:03:24,710 --> 00:03:30,170 So as soon as the value of government become Miklaszewski, you will update the answer and you will 50 00:03:30,170 --> 00:03:30,540 return. 51 00:03:31,880 --> 00:03:39,050 And finally, the answer will contain the Naude no data and you will return the answer simple. 52 00:03:40,970 --> 00:03:43,480 And we will discuss the industry's complexity. 53 00:03:43,490 --> 00:03:44,390 So time is big. 54 00:03:44,390 --> 00:03:51,560 Often in worst case you will you have to go through each and every node and space complexity again if 55 00:03:51,560 --> 00:03:53,540 the BSD as coutry. 56 00:03:53,810 --> 00:03:56,900 So space complexity will also be big often. 57 00:03:57,350 --> 00:03:57,700 Right? 58 00:03:57,980 --> 00:04:00,080 So these are the time and the space complexity. 59 00:04:00,920 --> 00:04:02,740 So this is all about this problem. 60 00:04:02,780 --> 00:04:05,160 Guys, I will see you in the next one. 61 00:04:05,300 --> 00:04:05,840 Thank you.