- 2 days ago
Transcript
00:00When the diameter is calculated, what is the first approach?
00:04We wrote the return type in int.
00:07Int is returned.
00:08At every level, I had a diameter.
00:12I called the diameter.
00:14I had left sub-tree and right sub-tree.
00:20But this time, we will return some extra information.
00:25How will we return a bundle of information?
00:29We will return the information.
00:31I will return a class of static class.
00:35In which I will return two things.
00:38One is my diameter.
00:40One is my height.
00:42For this time, I will call recursive function.
00:47This time, I will return the height.
00:49I will return the same recursive function.
00:52So, this time, the return type will be done.
00:57This time, we will return the information class.
00:59This time, we will return two information.
01:04Diameter is the first time.
01:06This time, we return the height.
01:09This time, we return the height.
01:22This time, there will be two number-tree and height.
01:25So, we'll return the number-tree and height.
01:49How can we do this?
01:50my self diameter
01:51and height what will happen
01:53is that height will happen
01:55is that maximum of left height
01:58plus right height plus
01:591. So diameter
02:01calculate and height
02:04calculate. Now we will
02:05do the same function
02:07and this is why
02:09our time complexity is reduced.
02:12Now I have to calculate the height
02:14because my sub trees
02:16or my children nodes
02:18they are already given height and diameter
02:20so I will also take my height and diameter
02:23and pass it to my parent.
02:26So the pseudo code is something like this
02:28that if I have taken my left
02:32information
02:32for the diameter
02:37calculate
02:37I have to take my root
02:39and take my left information
02:40and take my right information
02:43when I will call the diameter function
02:45which I will pass the root
02:47right. Left key information
02:49can under
02:49kya hai?
02:501 left key diameter
02:51and 1 left key height
02:52right key information
02:53can under
02:53kya hai?
02:541 right key diameter
02:55and 1 right key height
02:57hai? To my kya kya kar sakti ho?
02:58Mere tree ki jho final diameter
03:00aayegi
03:01that will be equal to
03:03maximum of
03:04mere left info
03:06dot diameter
03:08my right info
03:09dot diameter
03:11and
03:11my left height
03:13plus my right height
03:14plus 1
03:43and
03:44yahaan se yi jho puri information
03:44I will return myself to myself, if I have to return myself to myself, then I will return my information
03:48to myself, so I will return both values.
03:51So, I will return a new information, which will be my final diameter and my final height.
03:58So, our code will remain in this way, when I have to calculate the height, I will not have to
04:05calculate the same time,
04:05So, if I have to return the diameter, then I will return the information to myself and then I will
04:11return the entire diameter.
04:12So, I will try to dry run, so 4 will return my left height to myself, left diameter to 1
04:19equal, left diameter to 1 equal,
04:215 will return my left height to myself, and then I will return my left height to myself, which will
04:28be equal to 2.
04:29and left diameter ko return karega that is equal to 2. 6 return karke dega apni right diameter 1 and
04:36apni right height 1. 3 return karke dega apni right diameter 2 and apni right height 2.
04:42ab 1 ke paas joh final apni diameter aeggi that will be equal to 5 and yeh bhi apni height
04:48bhi saath mein return karke deta hai that is equal to 3.
04:51abhi sudo code ko hum actual code ki form mein convert karne waale hai.
04:54मैंने kya kiya tha? मैंने पहले अपना पूरा diameter function लिखा था.
04:58जिसके अंदर मैंने diameter and height ko alag-alag si calculate kiya tha.
05:02इसको मैं diameter 2 नाम दे देती हूँ.
05:04और मैं एक और function लिखती हूँ diameter के नाम से.
05:08public static info diameter
05:15अब information के लिए मैं एक static class बना दूँगी.
05:18static class info information के लिए में दर क्या क्या होना चाहिए?
05:24एक तो मेरी diameter उनी चाहिए.
05:26अद दूसरा मेरे पास height आ जाए.
05:30इसके लिए बना देते हैं एक constructor जिसके अंदर मेरे पास diameter आएगी and आएगी मेरी height.
05:37तो this.diameter is equal to diameter this.height is equal to my height.
05:47इस तरीकी की information class मैंने बनाईए और इस class के object को मैं return करूँगी.
05:52मैंने अपना diameter नाम का function मनाए है जिसके अंदर पास कर दूँगी अपनी root node.
05:58अब सबसे पहले क्या होगा?
05:59सबसे पहले मुझे अपनी left की information को calculate करना है.
06:03तो my info of left info is equal to diameter को मैं call कर दूँगी root के left के लिए.
06:11तो मेरी left की पूरी information आ जाएगी.
06:13उसके बाद मैं information को calculate करूँगी अपने right की information के लिए.
06:17तो मैं call कर दूँगी diameter of root dot right को.
06:21दोनों की information मेरी पास आगी left subtree की भी right subtree की.
06:25अब मुझे क्या करना है? अपनी information को बनाना है.
06:27तो मैं एक नई object बना लेती हूँ.
06:29सबसे पहले तो मैं अपनी diameter calculate कर लेती हूँ.
06:33int diameter will be equal to math dot maximum of left info dot diameter right info dot diameter
06:48and left info dot height plus right info dot height plus 1.
06:58यह क्या आया मेरी पास?
07:00diameter जो आई, वो आई maximum value of
07:03पहले मैंने left की diameter की value को compare कर लिया right की diameter की value से
07:08and फिर मैंने अपनी diameter निकाली which is left की height plus right की height plus 1.
07:13यहाँ पे मैं लगा देती हूँ math dot max एक और बार
07:17क्यूंकि तीन variables को compare नहीं कर सकता function.
07:19तो पहले यह दोनो variables compare हो जाएंगे, फिर इसकी value compare हो जाएगी, इससे मुझे मिल जाएगा
07:25अपना maximum.
07:26इसके अलावा मेरी जो height हो जाएगी, that will be equal to math dot maximum of left info dot height
07:35and right info dot height plus 1.
07:39तो मैं finally return कर दूँगी अपनी information, तो new object, new info, जिसमें pass कर दूँगे अपनी diameter and
07:49height.
07:50तो कुछ इस तरीके से मुझे चीजें करनी है, जहां सबसे पहले left की पूरी information निकाल दो, फिर right
07:55की पूरी information निकाल लो.
07:57उसके बाद अपनी diameter निकाल लिया, अपनी height निकाल लिया और return कर दीनी है, अपनी information in the form
08:02of an object, जहां पर मैं pass करूँगे, constructor में अपनी diameter and height.
08:05यहां के लिए base case क्या होगा, base case यह होगा, जब मेरी root नल हो जाती है, मतलब यहां
08:10तो मैं leaf node के किसी child पर पहुंच गई हूँ, यहां फिर मेरा tree completely खा ली है, उस
08:15case में, यानि null node के लिए, मेरे पास diameter भी 0 होती है, और मेरे पास height भी 0
08:19होती है, तो मैं return कर दूँगे, यहां से new info
08:22जिसके अंदर diameter is also 0 and height is also 0, अब यहां में notice किया होगा, पिछले वाला जो
08:27function था, उसमें मेरे height के लिए अलग से call लगाई थी, जिसके वज़े से time complexity बढ़ी थी, बट
08:33इस बार single node के उपर बैठ कर मेरी diameter भी वहीं calculate हो रही है, मेरी height भी वहीं
08:38calculate हो रही है, जिनको calculate करने में मु�
08:52इस बार height को मैंने diameter के साथ साथ calculate किया है, इसको कर लेते हैं save and main function
08:57से call लगाते है, diameter of root के लिए, इस बार जो मेरी diameter of root return करेगा, वो मेरी
09:05information object होगी, तो object के मुझे diameter return करवानी है, तो मैं use करूँगे अपने dot operator का, diameter
09:13of root मुझे return करेगा information object जिसमें से मैं diameter को print करवाऊंगी, इसको कर
09:18करते है, save and run, मेरे पास diameter आईए, अपने tree की equal to 5, तो इस tree की diameter
09:25मेरे पास 5 की equal, जैसे मैंने diameter को print कर आया है, वैसे मैं height को भी print कर
09:29आ सकती हूँ, तो साथ के साथ मेरे tree की height भी calculate हो जाएगी, which is equal to 3.