- 2 days ago
Transcript
00:00When we calculate the diameter in the first approach, what do we do in the first approach?
00:04We write our return type as int.
00:07Int is what we do in return.
00:08In every level, we have a return return.
00:11We have a diameter return.
00:13We call diameter, then we have a left sub-tree,
00:17and we have a right sub-tree.
00:20But this time, we will try to return some extra information.
00:25And how do we do that?
00:26We return a bundle of information.
00:30I will return a class of static class information.
00:36In which I will return two things.
00:38One will be diameter and one will be height.
00:42For this time, I will call recursive function.
00:47This time, I will return the height with the same recursive function.
00:51I will calculate it and return it.
00:54This time, the return type is going to happen.
00:57This is the information class.
00:59The information class is a object.
01:01We return two information.
01:03There will be two information.
01:03The diameter is the one we had before.
01:06We return the height.
01:08We return the height.
01:09So, what do we do?
01:104-2, will also be the diameter.
01:12And if your height could return the height.
01:14And if you will return the height for our left child,
01:16the height.
01:18And if your height's left child and height,
01:20and height is the height of the height,
01:20then we can also calculate the height of the height.
01:25How do we calculate the height?
01:28this node has its left diameter and its left height
01:31plus its right diameter and its right height
01:35so if I have any root diameter
01:39then it will be maximum of my left diameter
01:44my right diameter and left height plus right height
01:47plus one. This is my self diameter
01:52and what will be height?
01:53height will be maximum of left height
01:58plus right height plus one
02:00so diameter calculate
02:02and height calculate
02:04now we are going to do the same function
02:07and this is why
02:09our time complexity is reduced
02:11now I have to calculate height
02:13because my sub trees
02:16or my children nodes
02:18are already giving me height and diameter
02:20so what will I do?
02:22I will remove my height and diameter
02:23and I will pass it
02:25so the pseudo code will be
02:27that if at any point
02:29I have taken my left
02:32information
02:32for the diameter
02:37I have sent my left root
02:39and left information
02:59that will be equal to
03:03maximum of my left info dot diameter
03:08my right info dot diameter
03:10and my left height plus my right height plus one
03:24which will be equal to
03:40which will be equal to
03:44so I will return
03:44so I will return
03:46so I will return
03:47so I will return
03:49so I will return
03:50a new information
03:52which will be my final diameter
03:55and my final height
03:57so I will return
04:02this height
04:02I will have to calculate
04:03this time I will return
04:07and I will return
04:08and I will return
04:11and I will return
04:11and I will return
04:13so I will return
04:14so I will return
04:164
04:16left height
04:175
04:21right height
04:222
04:26left height
04:27which will be equal to
04:292
04:326
04:336
04:33my right diameter
04:351
04:35and my right height
04:363
04:37now
04:38to a final height
04:44that will be equal to
04:46and your height
04:48almost return
04:503
04:51now I have同ist code
04:52convert
04:53what
04:55What did I do? I wrote my whole diameter function in which I had diameter and height
05:00different calculated. I will give it the diameter to name it and I will write another function
05:05for diameter. Public static info diameter. Now for information, I will make a static class
05:17bana dungi. Static class. Info. Information ke lindar kya kya hona chahi? Ek to miri diameter
05:25honi chahi and dousra miri paas height a jaye. Iske lii bana dete hai ek constructor
05:33jiske and dar miri paas diameter aegi and aegi miri height. To this dot diameter is equal
05:41to diameter this dot height is equal to my height. This is the way information class
05:49I will return this class to this object. I will return my diameter name function in which
05:54I will pass my root node in which I will pass my root node in which I will pass my
05:57root node.
05:57First of all, I have to calculate my left information. My info of left info is equal to diameter
06:08I will call my root node. So my left information will come. Then I will calculate the information
06:15for my right information. So I will call my diameter of root dot right. The two information
06:22will come to me. Left sub-tree and right sub-tree. Now I will make my information as well.
06:27So I will make a new object. First I will calculate my diameter. Int diameter will be equal to
06:34math dot maximum of
06:41left info dot diameter
06:44right info dot diameter
06:48and left info dot height plus
06:53right info dot height plus one
06:56यह कiamente현़क हैं अगने एक्पार
07:07यह पहें वाहते हमां पही मैं लगा देती हूं
07:15math.max
07:16one more time
07:17because three variables
07:18compare not to the function
07:19so first these two variables
07:21compare will be
07:22then this value will be
07:23compare will be
07:23this will get my maximum
07:26this will get my height
07:27that will be equal to
07:29math.maximum of
07:34leftinfo.height
07:35and rightinfo.height
07:38plus one
07:39so I finally return
07:41my information
07:43to new object
07:44new info
07:47which I will pass
07:48my diameter
07:49and height
07:50so I will pass
07:51some things
07:52first of all
07:53left of information
07:54then right of information
07:56after that
07:57I will pass
07:58my diameter
07:58and height
07:59and return
08:00my information
08:01in the form of an object
08:02where I will pass
08:03my diameter and height
08:05here for the base case
08:06this will be
08:08when my root
08:09null goes
08:09either I have
08:11reached a leaf node
08:11to a child
08:12or my tree
08:42completely
08:44approach
08:44which I will pass
08:44under my time complexity
08:45it will be linear time complexity
08:47which I will pass
08:48because this time
08:49I will not calculate the height
08:52I have to calculate this with diameter.
08:55I will do save and main function
08:57for diameter of root
09:01This time, which will return the diameter of root
09:04it will be my information object.
09:07I will return the diameter of the object
09:10I will use the dot operator
09:12I will return the diameter of root
09:14which will be my information object.
09:17I will do save and run
09:20I will return the diameter of my tree
09:23equal to 5
09:24I will return the diameter of 5
09:26I will return the diameter of the height
09:28I will return the height
09:32which will be equal to 3