1 00:00:01,220 --> 00:00:01,890 Hi, everyone. 2 00:00:01,910 --> 00:00:05,130 So today we are going to solve this question in work by military. 3 00:00:05,990 --> 00:00:10,860 OK, so this is basically a binary tree and we have to invert this tree. 4 00:00:10,920 --> 00:00:13,940 OK, so what is the meaning of inverting a binary tree? 5 00:00:14,390 --> 00:00:18,110 So inverting means basically draw a line in the center line. 6 00:00:20,140 --> 00:00:21,460 OK, now what will happen? 7 00:00:21,670 --> 00:00:24,230 So basically three will come here and two will come here. 8 00:00:24,250 --> 00:00:28,930 So first of all, one in two will be swept to entry, will be swept. 9 00:00:28,960 --> 00:00:33,500 So three and two, then six will come here and five will come here. 10 00:00:33,520 --> 00:00:35,420 So basically, this will become six. 11 00:00:35,620 --> 00:00:36,570 This will be five. 12 00:00:36,910 --> 00:00:39,890 And then similarly, four and seven will be stabbed. 13 00:00:40,000 --> 00:00:44,470 OK, so seven will come here and four will come here. 14 00:00:45,100 --> 00:00:46,800 OK, let's take one more example. 15 00:00:47,200 --> 00:00:48,940 So we have to invert this binary. 16 00:00:49,180 --> 00:00:50,890 First of all, let us draw the line. 17 00:00:51,350 --> 00:00:52,780 OK, now what will happen. 18 00:00:52,790 --> 00:00:54,520 So we will step two in seven. 19 00:00:54,550 --> 00:00:58,100 OK, so this will become seven and the system. 20 00:00:58,570 --> 00:01:00,310 Now we will swap three and six. 21 00:01:00,790 --> 00:01:02,110 So six will come here. 22 00:01:02,290 --> 00:01:03,110 Three will come here. 23 00:01:03,370 --> 00:01:05,060 Now we will swap one and nine. 24 00:01:05,129 --> 00:01:08,380 OK, so nine will come here and one will come here. 25 00:01:08,440 --> 00:01:11,470 OK, so this is basically the inverted binary. 26 00:01:13,030 --> 00:01:19,810 OK, this is invited by Nutri, so the input is basically this by the input will be this tree and we 27 00:01:19,810 --> 00:01:22,270 have to return this inverted by Nutri. 28 00:01:22,900 --> 00:01:24,990 OK, so I hope you understood the question. 29 00:01:25,210 --> 00:01:26,520 Now let us know. 30 00:01:26,540 --> 00:01:28,550 Let's discuss how we can solve this question. 31 00:01:28,630 --> 00:01:32,470 OK, so this is a very simple question now how we can solve this question. 32 00:01:32,470 --> 00:01:34,330 So let us consider root. 33 00:01:34,840 --> 00:01:37,540 So in the root what we have. 34 00:01:37,570 --> 00:01:41,850 So this is basically subtree, this is basically the right subtree. 35 00:01:42,760 --> 00:01:48,760 So basically what we will do, we will swab the left subtree and we will swab the right separately and 36 00:01:48,760 --> 00:01:51,410 then we will call the recursion on left subtree. 37 00:01:51,460 --> 00:01:53,590 Then we will call the coalition on right subtree. 38 00:01:54,210 --> 00:01:56,200 OK, very simple. 39 00:01:56,560 --> 00:01:59,120 Let's Bridon, on this example. 40 00:01:59,170 --> 00:02:02,710 OK, so I have four, then two, then seven. 41 00:02:03,310 --> 00:02:04,630 Then one, three. 42 00:02:05,260 --> 00:02:06,250 Six and nine. 43 00:02:07,360 --> 00:02:12,220 So what we'll do, first of all, so this is left subtree and this is right. 44 00:02:13,090 --> 00:02:18,100 So first of all, swipe left and right, Sabari, we have two separate left and right. 45 00:02:18,100 --> 00:02:23,550 So it will look like this four, seven, six, nine. 46 00:02:23,770 --> 00:02:27,490 Then I have basically two, then one and then three. 47 00:02:28,160 --> 00:02:31,240 OK, so now this is the Route seven is the route. 48 00:02:31,390 --> 00:02:35,800 So SREP so seven, this is the subtree and this is the right subtree. 49 00:02:36,400 --> 00:02:41,320 Similarly for two, one is the left and three right sabari. 50 00:02:41,800 --> 00:02:42,790 So we will swap. 51 00:02:44,190 --> 00:02:46,970 We will swipe left and right. 52 00:02:48,900 --> 00:02:51,100 OK, so let's six and nine. 53 00:02:51,150 --> 00:02:54,320 OK, so nine will come here, it will become six. 54 00:02:54,330 --> 00:02:59,260 Similarly, let's wrap the left subtree so it will become three and it will become one. 55 00:02:59,560 --> 00:03:01,140 OK, so very simple. 56 00:03:01,860 --> 00:03:10,260 We just have to step left and right subtree and we will call the N word function on the left and right 57 00:03:10,260 --> 00:03:10,740 separately. 58 00:03:10,810 --> 00:03:14,940 OK, so let's write the code and then you will be able to understand it in a better way. 59 00:03:15,000 --> 00:03:15,300 OK. 60 00:03:16,430 --> 00:03:20,460 So we have to return basically the root of the inverted banditry. 61 00:03:20,540 --> 00:03:23,660 OK, so first of all, best case. 62 00:03:24,540 --> 00:03:26,480 So basically if rotational. 63 00:03:29,800 --> 00:03:34,560 Basically, two did not exist, so we can take leader dandled, there's no need to do anything. 64 00:03:34,570 --> 00:03:34,900 OK. 65 00:03:36,520 --> 00:03:40,840 Now, what we have to do, we have to step left and right subtree, so ASEP. 66 00:03:42,980 --> 00:03:49,460 They lived separately and wrote separate, so separate is inbuilt function, so I will pass left and 67 00:03:49,460 --> 00:03:50,160 right subtree. 68 00:03:50,210 --> 00:03:56,480 OK, so after stepping what we have to do, we have to call this inventory function on subtrend right 69 00:03:56,480 --> 00:03:56,910 subtree. 70 00:03:57,620 --> 00:04:03,410 OK, so let's call inventory three function on left subtree. 71 00:04:08,580 --> 00:04:14,830 OK, so dysfunction, this inverted function, this will return me the updated value of Lifespring, 72 00:04:14,940 --> 00:04:17,950 so I will store that updated value of subtree. 73 00:04:19,310 --> 00:04:20,209 And similarly. 74 00:04:21,490 --> 00:04:24,340 Let us call in order to function on right subtree. 75 00:04:29,970 --> 00:04:35,540 Now, this inventory function, it will update the right subtree and it will be done right separately. 76 00:04:35,550 --> 00:04:37,640 So let's update the right subtree, OK? 77 00:04:39,700 --> 00:04:41,890 Now, finally, our tree has been. 78 00:04:42,890 --> 00:04:50,120 So finally, our tree has been inverted and we can likely now we can return root, OK, because our 79 00:04:50,120 --> 00:04:51,350 tree has been inverted. 80 00:04:52,460 --> 00:04:53,810 So in order to send our cold. 81 00:04:57,150 --> 00:04:58,500 OK, so now let's admit. 82 00:05:02,450 --> 00:05:04,220 OK, so our goal is working fine. 83 00:05:05,090 --> 00:05:06,590 Now let's see how it is working. 84 00:05:06,770 --> 00:05:08,240 Let us consider an example. 85 00:05:09,290 --> 00:05:10,970 OK, so I have this appre. 86 00:05:12,800 --> 00:05:13,700 When three. 87 00:05:15,150 --> 00:05:20,550 Four, five, six and seven, OK, and this is my old. 88 00:05:22,680 --> 00:05:26,070 This is my route, so this is the best case route is not. 89 00:05:26,400 --> 00:05:28,680 Then I will swipe left and right. 90 00:05:29,160 --> 00:05:31,400 So after stepping, it will look like this. 91 00:05:31,420 --> 00:05:38,730 So when so I have three here, I have six, seven, and here I have two, four and five. 92 00:05:39,180 --> 00:05:43,320 OK, then I'm calling the inventory function on the left separately. 93 00:05:43,410 --> 00:05:51,540 OK, so this tree will be passed to the inventory function and similarly so in this line, what will 94 00:05:51,540 --> 00:05:52,590 happen to left. 95 00:05:52,800 --> 00:05:58,260 So basically this is the after forty six and seven is the right subtree. 96 00:05:58,440 --> 00:06:00,450 OK, so they will be swept. 97 00:06:01,650 --> 00:06:05,380 So after stepping, seven will come here and six will come here. 98 00:06:05,640 --> 00:06:09,520 Then I will call the inventory function on this part on seven. 99 00:06:09,570 --> 00:06:10,940 OK, so four seven. 100 00:06:10,950 --> 00:06:12,050 Levasseur Presnell. 101 00:06:12,060 --> 00:06:14,670 Right, separation if it will sweep null with null. 102 00:06:16,420 --> 00:06:17,960 They are not any changes, OK? 103 00:06:18,340 --> 00:06:20,830 No changes will be there, then it will return. 104 00:06:21,280 --> 00:06:24,780 So I will come to three, then three will call on the right side. 105 00:06:24,790 --> 00:06:26,230 I'm calling on the right subtree. 106 00:06:26,710 --> 00:06:28,990 So for six, what it will do. 107 00:06:29,020 --> 00:06:30,610 So it's a Presnell. 108 00:06:31,000 --> 00:06:31,600 Its right. 109 00:06:31,730 --> 00:06:35,150 There is also, if you will, step left and right subtree both undone. 110 00:06:35,170 --> 00:06:38,050 So no changes will be there then six will return to three. 111 00:06:38,260 --> 00:06:43,260 OK then three will return to one and then one, then the call will go to two. 112 00:06:43,300 --> 00:06:46,980 OK, so for two goals fape it's left and right subtree. 113 00:06:47,020 --> 00:06:54,430 So here Pfeifle come in here for Willkomm then five will call on left and right subtree so it will shrapnels 114 00:06:55,150 --> 00:07:01,220 then five will return then two will now call will go to four, then four will call on its territory 115 00:07:01,270 --> 00:07:01,810 which is null. 116 00:07:02,320 --> 00:07:04,660 It will call on to try to separate which is agonal. 117 00:07:04,990 --> 00:07:08,560 So Nele will be subdivisional so no changes. 118 00:07:08,740 --> 00:07:12,590 And finally I am returning the ok finally I am returning RWD. 119 00:07:12,640 --> 00:07:13,810 So when will we return. 120 00:07:15,300 --> 00:07:19,800 OK, so this is how this is working, so the time complexities, because often we have to go through 121 00:07:19,800 --> 00:07:20,640 each and every node. 122 00:07:20,670 --> 00:07:21,050 OK. 123 00:07:23,310 --> 00:07:26,350 So simple, so if you have any doubt in this question, you can ask me. 124 00:07:26,370 --> 00:07:26,790 Thank you.