1 00:00:01,420 --> 00:00:02,050 Hi, everyone. 2 00:00:02,080 --> 00:00:06,290 So in this video, we're going to solve this question, identical binary. 3 00:00:06,700 --> 00:00:08,650 OK, so this is the problem statement. 4 00:00:08,900 --> 00:00:14,860 So given to minorities, what we have to do, we have to check whether the two binary trees are equal 5 00:00:14,860 --> 00:00:15,340 or not. 6 00:00:15,430 --> 00:00:17,980 OK, so what is the meaning of equal to equal means? 7 00:00:17,980 --> 00:00:21,920 Their value should be C each value should be the same and destructive should be. 8 00:00:22,240 --> 00:00:27,730 OK, I will call to Binit recall when all the values are the same and the structure of the two minorities 9 00:00:27,730 --> 00:00:31,620 are saying, OK, let us consider this example so to buy. 10 00:00:31,960 --> 00:00:32,830 So this is driven. 11 00:00:33,780 --> 00:00:38,910 And this is true, too, you can see, so driven by the values are all the same and the structures also 12 00:00:38,910 --> 00:00:40,620 seem so, in this case I will return. 13 00:00:40,620 --> 00:00:40,950 True. 14 00:00:41,040 --> 00:00:43,070 OK, so that will be billion. 15 00:00:43,800 --> 00:00:45,330 I have to return the value. 16 00:00:45,330 --> 00:00:49,630 Whether there will buy new trees, I recall, or to buy any trees are not equal. 17 00:00:49,680 --> 00:00:49,960 OK. 18 00:00:49,980 --> 00:00:50,700 True or false. 19 00:00:51,740 --> 00:00:53,130 Let us consider this example. 20 00:00:53,360 --> 00:00:57,150 So basically the same one went to two, but the structure is different, OK? 21 00:00:57,170 --> 00:01:00,620 So we're here to light in the laboratory and here to it. 22 00:01:00,650 --> 00:01:01,340 Right subtree. 23 00:01:01,550 --> 00:01:03,250 So in this case, I will return false. 24 00:01:03,440 --> 00:01:05,510 They are not identical by nuclear. 25 00:01:05,680 --> 00:01:11,320 OK, let us consider this example so they doctor same structure structuralism. 26 00:01:11,810 --> 00:01:19,220 Now one one they are saying, OK, this no see here to the left hand side, but here too is present 27 00:01:19,220 --> 00:01:20,140 in the right hand side. 28 00:01:20,180 --> 00:01:21,290 So the values are different. 29 00:01:21,470 --> 00:01:26,600 OK, all of the structure, same structure, same this one root node then left and right. 30 00:01:26,600 --> 00:01:29,240 And similarly, this also has the same structure. 31 00:01:29,310 --> 00:01:30,640 OK, same. 32 00:01:30,680 --> 00:01:32,950 But the values are different here to this present here. 33 00:01:32,960 --> 00:01:36,130 One is Besant similarly in the right to play here when it's present. 34 00:01:36,140 --> 00:01:37,290 But here who is present. 35 00:01:37,520 --> 00:01:39,380 So in this case, I will return false. 36 00:01:40,690 --> 00:01:43,690 OK, so the question is very simple, given to minorities. 37 00:01:43,720 --> 00:01:47,260 I have to check, I have to return to boolean value, whether to recycle or not. 38 00:01:47,590 --> 00:01:49,180 So this is a very simple question. 39 00:01:49,240 --> 00:01:54,000 OK, so how we can solve this question so we can solve this question with the help of recursion. 40 00:01:54,070 --> 00:01:57,970 OK, we can very easily solve this question with the help of recursion. 41 00:01:58,180 --> 00:01:59,440 So consider Daughtry's. 42 00:01:59,440 --> 00:02:06,100 So this is driven the size of the plane ride subtree and similarly, I have to do so. 43 00:02:06,130 --> 00:02:07,460 It will have its left and right. 44 00:02:07,460 --> 00:02:08,020 Sudbury's. 45 00:02:08,080 --> 00:02:12,700 OK, so when these two trees will be equal, very simple condition. 46 00:02:13,240 --> 00:02:15,530 So everyone's left should be close to. 47 00:02:15,610 --> 00:02:21,210 So first is basically the left should be close to the left. 48 00:02:21,610 --> 00:02:23,470 Second is basically the ones right. 49 00:02:23,470 --> 00:02:24,850 Should be close to Titos. 50 00:02:24,850 --> 00:02:25,160 Right. 51 00:02:25,780 --> 00:02:26,710 So dittos. 52 00:02:27,990 --> 00:02:33,540 Divorce rate should be close to details, right, and the third is basically the value should be same. 53 00:02:33,570 --> 00:02:37,200 OK, so basically the one value should be close to it. 54 00:02:37,230 --> 00:02:43,050 Well, OK, I'm repeating myself when the Daughtry's will be equal. 55 00:02:44,650 --> 00:02:48,910 When the two trains will be equal there, a very simple, simple condition, their values should be 56 00:02:48,930 --> 00:02:51,550 same, the value should be equal to value. 57 00:02:52,850 --> 00:02:59,610 Even left subtree should be close to details left Saberi, Devens right, Saberi should be close to 58 00:02:59,610 --> 00:02:59,930 details. 59 00:02:59,940 --> 00:03:00,660 Right, Saberi. 60 00:03:00,680 --> 00:03:01,010 OK. 61 00:03:03,220 --> 00:03:07,590 Now, how we can check whether we are equal or not, basically we will call recursion on it. 62 00:03:08,700 --> 00:03:11,490 We will call recursion on it simple. 63 00:03:11,910 --> 00:03:14,280 OK, we can write the code. 64 00:03:14,310 --> 00:03:16,390 OK, so now let us likely write the code. 65 00:03:17,460 --> 00:03:19,380 So this is the question, same tree. 66 00:03:19,430 --> 00:03:25,200 OK, so after Rotana billion value, the name of the function is is same tree and it is sticking to 67 00:03:25,200 --> 00:03:27,450 trees as input B and given the two trees. 68 00:03:27,480 --> 00:03:31,770 OK, so let's change the name and be OK. 69 00:03:33,210 --> 00:03:34,530 And we are the Daughtry's. 70 00:03:35,500 --> 00:03:36,160 So first. 71 00:03:37,550 --> 00:03:44,030 Let us write very simple, simple condition, so we've basically driven Isdell, OK, three one does 72 00:03:44,030 --> 00:03:47,020 not exist and Preto does not exist. 73 00:03:47,180 --> 00:03:49,190 So basically reasonable. 74 00:03:50,710 --> 00:03:53,910 And tribesmen, if the tunnel, that means they are equal. 75 00:03:54,040 --> 00:03:57,100 So if they are equal, I can return to simple. 76 00:03:58,020 --> 00:04:00,330 Now, if everyone isn't Al. 77 00:04:03,140 --> 00:04:10,520 Driven, seasonal, but Priebke is not only so Truby is normal and reasonable. 78 00:04:10,550 --> 00:04:11,750 So this is very simple. 79 00:04:12,020 --> 00:04:14,830 That means that both the trees are not equal because one is none. 80 00:04:15,080 --> 00:04:16,160 Other one is normal. 81 00:04:16,670 --> 00:04:17,750 So I can return false. 82 00:04:19,120 --> 00:04:22,930 Similarly, the other way around IFPRI is Dardanelle. 83 00:04:24,950 --> 00:04:26,410 And drib is Nele. 84 00:04:27,340 --> 00:04:33,600 So if this is a scenario, then also I can return false because they will not be OK when this one is 85 00:04:33,610 --> 00:04:34,070 not null. 86 00:04:35,230 --> 00:04:41,580 Now, what is the condition that he and Drillbit they are seeing, so I will use these three conditions, 87 00:04:41,620 --> 00:04:43,870 OK, I will use these three conditions. 88 00:04:43,900 --> 00:04:46,800 So if all the conditions are true, I will return to those. 89 00:04:46,810 --> 00:04:47,650 I will return false. 90 00:04:47,680 --> 00:04:51,430 OK, so if all the conditions are true, I will return true. 91 00:04:51,430 --> 00:04:54,460 Otherwise I will return false if any of the condition is false. 92 00:04:55,270 --> 00:04:56,800 OK, so. 93 00:04:57,760 --> 00:05:01,750 Let's so what should happen so evalu. 94 00:05:03,340 --> 00:05:04,770 Should be close to be value. 95 00:05:05,910 --> 00:05:08,010 OK, their value should be simple. 96 00:05:08,870 --> 00:05:14,310 This is the first condition, second condition is basically their left subtree should also be seen. 97 00:05:14,360 --> 00:05:17,230 So what we will do, we will take the function of a cemetery. 98 00:05:17,280 --> 00:05:20,670 OK, this is a recursive function, so let's copy them. 99 00:05:20,670 --> 00:05:21,300 Function name. 100 00:05:24,870 --> 00:05:32,730 So is same tree, I will give it to trees, so I am giving the leaves a tree of tree and I'm giving 101 00:05:32,730 --> 00:05:33,940 the A of tree. 102 00:05:34,710 --> 00:05:37,970 OK, so this function is same tree function. 103 00:05:37,980 --> 00:05:42,460 This will be true or false, whether through trees e left and be left with the same or not. 104 00:05:42,480 --> 00:05:48,210 OK, this is recursive function and similarly I will call this function the same tree for the right 105 00:05:48,330 --> 00:05:48,780 price. 106 00:05:48,810 --> 00:05:53,880 OK, so I will, I have to give two trees so erate and be right. 107 00:05:56,380 --> 00:06:01,930 So we've basically all the three conditions are true if this condition is true, if this basically. 108 00:06:04,290 --> 00:06:09,630 So what is the work of dysfunction, so what is the work of is the same dysfunction? 109 00:06:10,050 --> 00:06:16,170 So it will take two days as input and B and it will return a boolean value, whether they're three a.m. 110 00:06:16,590 --> 00:06:18,090 or three a.m.. 111 00:06:18,280 --> 00:06:21,060 OK, so these are very simple, simple conditions. 112 00:06:21,510 --> 00:06:25,080 Now, what I'm doing here is so when are the two trees will be equal? 113 00:06:25,830 --> 00:06:27,240 When two trees will be equal? 114 00:06:28,540 --> 00:06:34,420 When their values are the same, so if the values of A, B, if their values the same, if they live 115 00:06:34,420 --> 00:06:37,870 surprising, so if they are left separately the same. 116 00:06:38,080 --> 00:06:42,760 So but I will do I will call this function the same tree and I will give the left's. 117 00:06:43,420 --> 00:06:45,750 I will give that left subtree of B OK. 118 00:06:45,790 --> 00:06:51,070 It will return true or false requestion will handle OK because it will handle everything. 119 00:06:52,080 --> 00:06:58,650 Now, again, there should also be some so I'm calling the function the same tree, I'm calling the 120 00:06:58,650 --> 00:07:00,960 function the same tree, and I am passing their rates up. 121 00:07:01,230 --> 00:07:04,640 So I'm giving the right set of I am giving the right set of me. 122 00:07:04,650 --> 00:07:07,440 OK, now every I'm using and operated. 123 00:07:07,470 --> 00:07:10,620 OK, so if all the conditions are true. 124 00:07:12,040 --> 00:07:15,560 That means and B, they both have saying I can return, true. 125 00:07:15,580 --> 00:07:16,390 I can return true. 126 00:07:16,390 --> 00:07:17,740 Otherwise I will return false. 127 00:07:17,750 --> 00:07:18,310 Simple. 128 00:07:20,550 --> 00:07:21,330 So what I will do. 129 00:07:24,520 --> 00:07:29,210 I will return true in the end part, what I can do. 130 00:07:29,230 --> 00:07:30,190 I can return false. 131 00:07:31,880 --> 00:07:32,390 Simple. 132 00:07:38,610 --> 00:07:41,000 OK, our goal is working fine, Alert summered. 133 00:07:45,460 --> 00:07:47,380 OK, so our goal is working fine. 134 00:07:48,190 --> 00:07:52,090 OK, now a little bit of time, complexity and the space complexity. 135 00:07:53,040 --> 00:07:58,840 So basically three letters, let's say this containing and notes and three B is containing amnot. 136 00:07:58,870 --> 00:07:59,260 OK. 137 00:08:00,700 --> 00:08:02,060 So these are the number of nodes. 138 00:08:02,920 --> 00:08:07,330 So what I'm doing here is I'm basically going through each and every note of it and I'm going through 139 00:08:07,330 --> 00:08:09,220 each and every note of be OK. 140 00:08:09,520 --> 00:08:11,430 So all the time complexity. 141 00:08:11,440 --> 00:08:13,450 So the time complexity will be emplacing. 142 00:08:13,630 --> 00:08:16,590 OK, I'm going through each and all of that. 143 00:08:17,590 --> 00:08:23,260 But in reality, the time complexity will be basically off minimum of Immonen. 144 00:08:24,710 --> 00:08:29,240 Because if the laws are different, OK, see if the laws are different, this is a.. 145 00:08:29,360 --> 00:08:30,590 This is a.. 146 00:08:30,630 --> 00:08:32,330 That means the structure is different. 147 00:08:32,990 --> 00:08:35,730 OK, that means the structure is different. 148 00:08:35,960 --> 00:08:38,659 So if the structure is different with the help of these two conditions. 149 00:08:39,840 --> 00:08:43,740 With the help of these two conditions, I will return false and my reaction will stop. 150 00:08:43,770 --> 00:08:48,090 OK, so that's why I have written here the minimum of Immonen. 151 00:08:48,780 --> 00:08:52,950 We will not explore the whole lot, so I will explore these nodes. 152 00:08:52,980 --> 00:08:54,500 OK, simple. 153 00:08:54,990 --> 00:08:57,910 So if you have any doubt in this question, you can ask me, ok. 154 00:08:58,740 --> 00:08:59,860 The code is very simple. 155 00:08:59,910 --> 00:09:01,950 OK, so if you have it out you can ask me. 156 00:09:01,980 --> 00:09:02,400 Thank you.