1 00:00:01,340 --> 00:00:05,540 Highly even so in this video, they're going to solve this question symmetric by Nutri. 2 00:00:06,520 --> 00:00:13,000 OK, so they will basically be a binary tree and we have to return, whether that by Newton's symmetric 3 00:00:13,030 --> 00:00:13,450 or not. 4 00:00:13,450 --> 00:00:16,820 OK, so basically the return type is basically a boolean value. 5 00:00:16,840 --> 00:00:17,530 I have to return. 6 00:00:17,530 --> 00:00:18,370 True or false? 7 00:00:19,150 --> 00:00:19,450 True. 8 00:00:19,450 --> 00:00:23,440 If the given tree is basically symmetric tree, otherwise I have to return false. 9 00:00:23,920 --> 00:00:28,420 OK, so let us consider an example and let's see what is a symmetric by nutri. 10 00:00:28,780 --> 00:00:30,280 So let us consider this example. 11 00:00:30,310 --> 00:00:32,710 So what I will do, I will draw a centerline. 12 00:00:32,890 --> 00:00:33,330 OK. 13 00:00:34,770 --> 00:00:41,190 Now, I will compare the values, I will compare the values, so in this case, so I will compare the 14 00:00:41,190 --> 00:00:42,450 value as well as the structure. 15 00:00:42,480 --> 00:00:44,310 OK, so I will compare. 16 00:00:45,990 --> 00:00:53,440 Value and structure, both should be seen on the left and the right hand side of this middle line. 17 00:00:53,540 --> 00:01:00,440 OK, now values I seem to want to and also distances seem if this distance is this distance is also 18 00:01:00,720 --> 00:01:01,080 OK. 19 00:01:01,260 --> 00:01:02,560 So value and distance assim. 20 00:01:03,360 --> 00:01:04,239 Now let's move ahead. 21 00:01:04,319 --> 00:01:09,240 Let's compare for to so for and for values the same distances also same BMV. 22 00:01:10,080 --> 00:01:11,650 Now compare three with three. 23 00:01:11,720 --> 00:01:16,350 OK, so if the distance is this distance is already ok. 24 00:01:16,380 --> 00:01:17,800 And again the values are same. 25 00:01:18,510 --> 00:01:22,950 So basically this tree, this tree is a symmetric tree so I will return. 26 00:01:22,950 --> 00:01:23,250 True. 27 00:01:23,890 --> 00:01:26,040 OK, now let us consider this tree. 28 00:01:27,170 --> 00:01:28,220 So industry. 29 00:01:29,460 --> 00:01:31,230 First of all, it does draw the line. 30 00:01:32,250 --> 00:01:37,710 And now let us compare the values and the structure, OK, values our same distances, also same let's 31 00:01:37,710 --> 00:01:38,670 call it the. 32 00:01:39,510 --> 00:01:43,150 So in this case, values are simply entry, but the states are different. 33 00:01:43,500 --> 00:01:45,650 So in this case, let's call it divine. 34 00:01:45,990 --> 00:01:48,080 And this distance is basically data. 35 00:01:48,190 --> 00:01:51,210 OK, so this is not a symmetric boundary, OK? 36 00:01:51,360 --> 00:01:52,410 This is not symmetric. 37 00:01:52,410 --> 00:01:55,560 So I will return calls for this by nutri. 38 00:01:56,320 --> 00:01:58,440 OK, so I hope you understood the question. 39 00:01:58,900 --> 00:02:03,120 Now suppose this is the root node now. 40 00:02:03,120 --> 00:02:03,540 Root node. 41 00:02:03,540 --> 00:02:04,650 This is the left subtree. 42 00:02:04,650 --> 00:02:06,030 Let's call it three one. 43 00:02:06,450 --> 00:02:08,280 This is the right subtree of the root. 44 00:02:08,490 --> 00:02:10,930 Let's call it due to OK, Rita. 45 00:02:11,130 --> 00:02:15,650 Driven driven to do so when this. 46 00:02:16,410 --> 00:02:19,170 So what is the condition for this tree to be a small victory. 47 00:02:20,040 --> 00:02:22,560 OK, so there are very small, small conditions. 48 00:02:23,010 --> 00:02:25,140 So first condition is basically. 49 00:02:26,070 --> 00:02:35,490 If the values of OK, so basically first condition is Wellinghoff, everyone's ruled is equal to the 50 00:02:35,490 --> 00:02:39,300 Valley of the Dolls to. 51 00:02:39,750 --> 00:02:40,150 OK. 52 00:02:40,650 --> 00:02:42,240 The second condition is basically. 53 00:02:43,500 --> 00:02:45,480 The events left subtree. 54 00:02:46,860 --> 00:02:50,540 Should be symmetric with details, right subtree. 55 00:02:51,060 --> 00:02:54,060 OK, so we can see here I am comparing. 56 00:02:54,070 --> 00:02:57,150 OK, so this is everyone, let's call it Reto. 57 00:02:57,990 --> 00:03:04,730 OK, so 41 if I'm calling with the left hand side, so 42, I will call on the right and say, OK, 58 00:03:04,890 --> 00:03:07,050 so 41, this is left 42. 59 00:03:07,050 --> 00:03:07,680 This is right. 60 00:03:08,350 --> 00:03:13,620 OK, so everyone's left should be close to it should be symmetric to deduce. 61 00:03:13,680 --> 00:03:14,040 Right. 62 00:03:14,850 --> 00:03:17,400 And a third condition is basically the ones. 63 00:03:17,400 --> 00:03:17,790 Right. 64 00:03:18,060 --> 00:03:20,550 Should be symmetric to reduce left. 65 00:03:21,660 --> 00:03:24,460 OK, so these are the very simple, simple conditions. 66 00:03:25,860 --> 00:03:32,820 Now, what we can do, we can we can make the help of recursion and we can solve this question with 67 00:03:32,820 --> 00:03:34,350 the help of these three conditions. 68 00:03:34,380 --> 00:03:39,240 OK, you can see here so 42 if I'm comparing four. 69 00:03:39,240 --> 00:03:43,650 So I will call on the right hand side, but 42 to compare for. 70 00:03:43,650 --> 00:03:45,570 I will call on the left hand side. 71 00:03:47,030 --> 00:03:50,390 OK, so I hope you understood the question. 72 00:03:50,420 --> 00:03:54,710 Now let us write the code, OK, remembering these three conditions. 73 00:03:57,450 --> 00:04:03,300 So basically, they of the function is asymmetric, we have to complete this function, then put as 74 00:04:03,300 --> 00:04:08,100 basically a tree and the return type is whether they're given trees symmetric or not. 75 00:04:08,280 --> 00:04:08,700 OK. 76 00:04:10,390 --> 00:04:11,830 So first of all, the base case. 77 00:04:13,150 --> 00:04:18,970 So if rotational, then I can say that given today's symmetrical, so I can return to. 78 00:04:24,250 --> 00:04:26,940 OK, so now let us create a helper function. 79 00:04:27,000 --> 00:04:28,270 OK, so. 80 00:04:29,540 --> 00:04:35,960 Let us create a helper function which will do our work so that it will obviously be able I have helper 81 00:04:35,960 --> 00:04:38,630 function, so it will take two tries to compare. 82 00:04:38,690 --> 00:04:39,050 OK. 83 00:04:40,770 --> 00:04:46,250 It will take two raise as input, so the left separates the left and the right tree. 84 00:04:51,740 --> 00:04:58,220 OK, so this helper function will do our work and what I will do, so let us call this function helper 85 00:04:58,220 --> 00:05:01,580 function and I will pass the left and the right subtree. 86 00:05:13,140 --> 00:05:17,130 OK, so what I am doing here is so basically I am creating a helper function. 87 00:05:17,580 --> 00:05:23,040 OK, and this helper function will take place as input and it will compare these duties. 88 00:05:23,070 --> 00:05:26,790 OK, so, for example, let us consider this. 89 00:05:27,240 --> 00:05:30,150 So this helper function will take this tree as input. 90 00:05:30,160 --> 00:05:34,710 So it is stored in the left variable and this tree will be stored in the right variable. 91 00:05:34,770 --> 00:05:36,840 OK, so I comparing these two trees. 92 00:05:37,910 --> 00:05:43,850 Two, three, four, and the right is basically two for three, OK, I am passing the left subtree and 93 00:05:43,850 --> 00:05:45,090 I am passing the right subtree. 94 00:05:45,470 --> 00:05:47,960 OK, so now let us complete the court. 95 00:05:48,140 --> 00:05:50,250 So the court is going to be very, very simple. 96 00:05:50,720 --> 00:05:54,740 Now, let us take care of a few situations, a few simple, simple cases. 97 00:05:55,620 --> 00:05:57,330 OK, so case number one. 98 00:05:57,950 --> 00:06:00,020 So if the left Presnell. 99 00:06:01,460 --> 00:06:03,750 And the right tree is not. 100 00:06:04,460 --> 00:06:10,090 OK, so basically the right tree is nocturnal, then they are not symmetric. 101 00:06:10,100 --> 00:06:11,570 So I can return false. 102 00:06:15,370 --> 00:06:15,880 Simple. 103 00:06:20,380 --> 00:06:21,220 Now, similarly. 104 00:06:22,920 --> 00:06:30,750 So if the left today is not OK, left pre-exist, but right, it does not exist, so right, reasonable 105 00:06:31,200 --> 00:06:34,680 and left is not null, then also I can return false. 106 00:06:36,000 --> 00:06:42,240 Simple now, if the values are different, OK, so if the value of lefty. 107 00:06:45,590 --> 00:06:48,140 It's not a close to the valley of right tree. 108 00:06:51,720 --> 00:06:54,030 Then in that case, I can also return false. 109 00:06:56,020 --> 00:06:56,600 Simple. 110 00:06:56,650 --> 00:07:01,670 OK, so if the levels are Presnell and right, if not, then return false. 111 00:07:02,140 --> 00:07:05,690 If the left is not all right, then then also return false. 112 00:07:06,040 --> 00:07:09,050 If their values are different, then also return false. 113 00:07:09,070 --> 00:07:10,100 They are not symmetric. 114 00:07:10,150 --> 00:07:14,260 OK, so force structure should be same and the values should also be the same. 115 00:07:15,740 --> 00:07:19,220 Now, there can be one more condition if the tunnel OK. 116 00:07:20,460 --> 00:07:23,790 If both left and right, so in that case, I can return to. 117 00:07:25,310 --> 00:07:28,910 So if left and right is also nil. 118 00:07:32,250 --> 00:07:33,780 Then I can return true safely. 119 00:07:35,040 --> 00:07:39,060 OK, so these are very simple, simple conditions now what we have to do. 120 00:07:39,420 --> 00:07:42,620 So I have to make use of these two conditions, OK? 121 00:07:43,200 --> 00:07:45,390 I will make use of these two conditions. 122 00:07:47,780 --> 00:07:53,330 We have already handled this one, OK, if you will see the code, we have already handled the condition. 123 00:07:53,370 --> 00:07:55,640 Now let us handle these two conditions. 124 00:07:55,670 --> 00:07:58,100 OK, I have to pass the left. 125 00:07:59,100 --> 00:08:03,790 And was right, and similarly, if he wins right, then it's left. 126 00:08:04,020 --> 00:08:04,370 OK. 127 00:08:07,220 --> 00:08:08,780 So the name of the function is helper. 128 00:08:10,880 --> 00:08:16,730 So, OK, I have to change the name, because you can see left and right have already been used. 129 00:08:16,760 --> 00:08:19,790 OK, so let's name it left three. 130 00:08:21,970 --> 00:08:23,170 And this is right, three. 131 00:08:44,390 --> 00:08:51,000 OK, so now that is called helper function, and we will take we will make use of these two conditions. 132 00:08:51,020 --> 00:08:51,350 OK. 133 00:08:52,890 --> 00:08:53,430 So. 134 00:08:54,640 --> 00:08:55,600 If left three. 135 00:08:58,080 --> 00:09:03,360 So if I'm giving the left three left, then I have to give right three, right. 136 00:09:06,690 --> 00:09:09,030 So these two trees should be symmetric and. 137 00:09:12,490 --> 00:09:13,570 If I'm giving. 138 00:09:16,220 --> 00:09:21,200 The left three right, then I have to pass the left of the right tree. 139 00:09:21,280 --> 00:09:21,620 OK. 140 00:09:25,810 --> 00:09:26,860 So right we left. 141 00:09:28,540 --> 00:09:29,230 Very simple. 142 00:09:32,560 --> 00:09:38,110 OK, so this is basically driven this is basically to do OK, so this is route. 143 00:09:39,490 --> 00:09:42,390 Everyone is this one and two is this one. 144 00:09:42,450 --> 00:09:45,220 OK, so these are very simple, four conditions. 145 00:09:45,360 --> 00:09:48,030 OK, now I'm calling the helper function. 146 00:09:48,330 --> 00:09:49,860 So for the helper function. 147 00:09:51,170 --> 00:09:57,470 If I am passing through, you once left, so Trillanes left, that means decide, OK, three months 148 00:09:57,470 --> 00:10:00,260 left, then I have to pass it right. 149 00:10:00,260 --> 00:10:01,520 So Britos right. 150 00:10:03,520 --> 00:10:09,730 OK, then only they will be at the same distance, so if this distance is deep, then also this distance 151 00:10:09,730 --> 00:10:13,140 will be OK and we are using and OK. 152 00:10:13,450 --> 00:10:16,410 And if I am comparing Even's, right. 153 00:10:16,810 --> 00:10:20,590 So this is the once right, then I have to compare it to the left. 154 00:10:20,620 --> 00:10:21,940 So this is due to the left. 155 00:10:23,360 --> 00:10:31,790 OK, this distance is deep, then only this distance will be OK if everything if this whole tree's symmetric 156 00:10:31,790 --> 00:10:38,210 to this tree we are using the end condition, then only I will return to OK, otherwise I will return 157 00:10:38,210 --> 00:10:38,570 false. 158 00:10:38,990 --> 00:10:40,440 OK, very, very simple. 159 00:10:40,760 --> 00:10:44,150 So what is the complexities or time complexities big often. 160 00:10:45,260 --> 00:10:48,470 And the space complexity is also big, often due to recursion. 161 00:10:49,880 --> 00:10:52,670 OK, so very simple and very logical. 162 00:10:53,390 --> 00:10:53,750 OK. 163 00:10:56,090 --> 00:10:57,740 OK, so now letters are now called. 164 00:11:03,120 --> 00:11:05,670 OK, so we are getting random error at this lane. 165 00:11:06,300 --> 00:11:07,110 Now let's see why. 166 00:11:10,000 --> 00:11:16,210 OK, so obviously we will get rain tomorrow, and I suppose if the left reasonable, if the left Presnell, 167 00:11:16,420 --> 00:11:18,660 then how can you assess the value of nil? 168 00:11:19,060 --> 00:11:23,260 And similarly, if the right reason, then how we can assess the value of nil. 169 00:11:24,790 --> 00:11:26,470 So that's why we are getting there in time. 170 00:11:26,790 --> 00:11:33,640 OK, so to solve this problem, what can I do so I can write this line below this line? 171 00:11:33,710 --> 00:11:38,310 OK, so I will write line number 16 before line number 15. 172 00:11:38,620 --> 00:11:41,440 OK, that will solve our problem. 173 00:11:42,280 --> 00:11:42,970 Now, let's see. 174 00:11:44,790 --> 00:11:53,640 So just remove this line and write this line the last, OK, so I have to write this line after this 175 00:11:53,640 --> 00:12:00,000 line because headline number 15, I am checking if left reasonable and retrieve the boat and then return. 176 00:12:00,000 --> 00:12:00,270 True. 177 00:12:00,350 --> 00:12:06,420 OK, so when we will reach line number 17, I am very sure that left, pre-exist and right also exist. 178 00:12:06,660 --> 00:12:09,030 OK, so now if you will run call. 179 00:12:11,390 --> 00:12:13,130 Our goal will work fine, hopefully. 180 00:12:16,690 --> 00:12:17,920 OK, now let's admit. 181 00:12:23,980 --> 00:12:27,030 OK, so our goal is working for success. 182 00:12:27,100 --> 00:12:30,640 OK, so if you have any doubt, you can ask me. 183 00:12:31,210 --> 00:12:31,690 Thank you.