• 2 days ago
Transcript
00:00Next question, which we will talk about is the name of the diameter of a tree.
00:03What is the diameter of a tree?
00:05What is the diameter of a tree?
00:06We will listen to this in circles.
00:08When we were taking a circle of a example,
00:10it was a radius of a double amount,
00:13which includes a circle of a circle,
00:17this is equal to the diameter.
00:19We also define a property in this tree,
00:22which we call diameter.
00:24If I talk about the proper definition of diameter,
00:26the diameter is defined as the number of nodes
00:32in the longest path
00:38between two leaves.
00:42This means, if I take a binary tree in my two leaves,
00:47and I calculate these two leaves,
00:52which leaves the longest path,
00:54the nodes of the nodes.
00:57What can I say?
00:58This tree, if I take an example,
01:00if I take this leaf and this leaf,
01:03then how much the path between these two?
01:05These two paths will be equal to this path.
01:09If I take 4 to 5,
01:10then how many nodes will I take?
01:12I take 1, 2, and 3.
01:15This is not the longest path.
01:16But if I take 4 to 4,
01:19and I want to reach to 6,
01:20then from 4 to 6,
01:22it will look like this.
01:24How many nodes will there be?
01:251, 2, 3, 4, 5.
01:27So, this way,
01:28the diameter of this tree will be 5.
01:31This diameter of 4 and 6,
01:33or 5 and 6,
01:36or 5 and 6,
01:38the same number of nodes will be equal to 5.
01:41So, I take the diameter of this tree,
01:44that is equal to 5.
01:46If I take the example of my second tree,
01:48then in this tree,
01:49the leaves,
01:50I try to remove the path between 9 and 3.
01:54This path will look like this.
01:57How many nodes will be?
01:581, 2, 3, 4, and 5.
02:01So, here,
02:01one possible value,
02:03which I have,
02:03that is equal to 5.
02:05The other possible value,
02:06is that I take this leaf
02:07and this leaf
02:08and this leaf.
02:09I take this path.
02:09That path will look like this.
02:13So, this node will be 1, 2, 3, 4, 5, 6.
02:16So, a parallel value will be 6.
02:19Now, what do I do?
02:20In this node,
02:21in this node,
02:22if I take these two leaves,
02:23if I take this path,
02:24it will be 1, 2, 3, 4, 5, 6.
02:29This value will be 6.
02:31What is the maximum value?
02:32What is the maximum value?
02:32Maximum is equal to 6.
02:34So, I can say that
02:35the diameter of this tree
02:38is equal to 6.
02:40Now, let's assume here,
02:42if I have another node,
02:44which is equal to 10.
02:45What happens in this case?
02:46This is my tree's diameter.
02:49This is fixed diameter.
02:50This is fixed diameter.
02:50And the diameter,
02:51in this case,
02:51has become 7.
02:53Which lies in this leaf
02:54and this leaf.
02:56When we calculate the tree's diameter,
02:58we usually have 2 cases.
03:00The first case comes from
03:02which is where my tree's diameter
03:03will go through through my root node.
03:06So, in this case,
03:07my tree's diameter,
03:09will be included my root node.
03:11Another case happens when
03:13the tree's diameter
03:14exists in some tree
03:15and it doesn't go through
03:17and it doesn't go through.
03:18So, if I take this tree's example,
03:20if I take this tree's line,
03:24the distance of leaves,
03:25will not become my diameter.
03:26My longest path will be
03:28between any two leaves.
03:30It will be this path.
03:31And in this path,
03:32my root node is not included.
03:35So, there are 2 special cases
03:36that I see.
03:37If I have another tree's root,
03:39then either my entire tree's diameter
03:42can go through
03:42or put it into some tree
03:44or put it into some tree.
03:46So, recursively,
03:47I have to calculate
03:48that in any other root,
03:49the diameter of the root
03:52can go through
03:52or exist in some tree.
03:55So, first of all,
03:57we will discuss the approach number 1.
03:59For the approach number 1,
04:02we will discuss the time
04:02for the number 1.
04:03with n2.
04:04This is the approach
04:06which we will discuss.
04:07The approach is something like this,
04:08that if I have
04:09any tree's root node
04:11and what I have?
04:12I have left to left
04:14sub tree's height
04:14and right to right
04:16sub tree's height.
04:17So, I can remove
04:18the diameter of the diameter
04:20which will go through.
04:21If I have left
04:23sub tree's height
04:24and right
04:25sub tree's height
04:26and diameter
04:27if I have root
04:28through,
04:29then I can say
04:30that my diameter
04:31will be equal to
04:32left height
04:32plus right height
04:33plus 1.
04:34Why?
04:35Because the diameter
04:35will include these nodes
04:36and these nodes
04:38and these nodes
04:38will include
04:39plus my additional node
04:41that I have to include.
04:42So, if I have
04:45left height
04:45and right height
04:46and I have this
04:48assumption
04:48that my diameter
04:50passes
04:52through root.
04:55So, in this case,
04:56my diameter
04:57will be equal to
04:58left height
04:59plus right height
05:00plus 1.
05:01And if I have
05:02another case
05:02in which
05:03diameter
05:05doesn't pass
05:06through root.
05:07So,
05:08in this case,
05:09my left
05:09sub tree
05:10will already
05:10calculate
05:10and my right
05:13sub tree
05:14will already
05:14calculate
05:15so,
05:16in this case,
05:18the maximum
05:18diameter
05:19diameter
05:19is the maximum
05:20diameter
05:20and then
05:21the maximum
05:25value
05:26will be
05:26compared
05:27to this value.
05:29I have
05:30left
05:32diameter
05:33and
05:34right
05:35left
05:35diameter
05:35and then
05:37compare
05:37these two values
05:38and compare
05:39these three
05:41maximum value
05:42will be
05:43diameter
05:44.
05:45I have
05:46something like
05:47this tree
05:47exists
05:48which is
05:48very
05:50nodes
05:51are
05:51If I am
05:54this node
05:55then
05:57what will
05:58do
05:58sub tree
05:59and
06:03diameter
06:03first
06:04first
06:05first
06:05I will
06:05to
06:06my left
06:06sub tree
06:06for
06:07diameter
06:07and
06:08then
06:09I will
06:11calculate
06:11this
06:11root
06:12and
06:15this
06:16tree
06:17will
06:17one
06:18one
06:18two
06:18three
06:18four
06:19five
06:19six
06:19seven
06:20equal
06:20here
06:21return
06:22value
06:22as
06:23seven
06:24and
06:24here
06:24I will
06:33return
06:34value
06:34as
06:46seven
06:47and
06:48height
06:48is
06:48four
06:49equal
06:49and
06:50right
06:50height
06:50is
06:50equal
06:51and
07:00right
07:01height
07:02is
07:02equal
07:04this
07:04value
07:04is
07:05equal to
07:05six
07:05and
07:05left
07:06diameter
07:07has
07:07already
07:09seven
07:09returned
07:10and
07:11right
07:12diameter
07:12has
07:14one
07:14returned
07:14so
07:15in
07:15three
07:16maximum
07:16will
07:17become
07:18diameter
07:18has
07:22single
07:23node
07:24left
07:25diameter
07:25says
07:25that
07:26my
07:26diameter
07:26should
07:27be
07:27included
07:27and
07:31diameter
07:31should
07:36be
07:37included
07:37and
07:38their
07:38values
07:39are
07:39six
07:39seven
07:40and
07:40one
07:40that
07:43will
07:44be
07:44equal
07:44to
07:44my
07:45left
07:45diameter
07:46this
07:46whole
07:46tree
07:47will
07:47be
07:48equal
07:49so
07:52when
07:53we
07:53have
07:53three
07:55choices
07:56the
07:56first
07:56choice
07:57is
07:57my
07:57tree
07:58diameter
07:59right
07:59and
08:05the
08:05choice
08:07is
08:07that
08:08the
08:08root
08:09is
08:10through
08:11pass
08:12these
08:12three
08:13values
08:13calculate
08:14each
08:15node
08:15and
08:16that
08:17will
08:19become
08:19the
08:19final
08:19diameter
08:20of
08:20my
08:20tree
08:20that
08:21will
08:22become
08:22the
08:22final
08:23diameter
08:23of
08:23then
08:23we
08:24will
08:24to
08:25see
08:25i
08:26have
08:27three
08:27every node
08:27i
08:27have
08:28three
08:28values
08:29i
08:29have
08:42to
08:53calculate
08:530
08:53and
08:53right
08:54height
08:54is
08:540
08:55so
08:55what
08:56will
08:564
08:56will
08:57see
08:57that
08:57my
08:58diameter
08:58should
08:58be
08:59equal to
09:11left
09:11height
09:12plus
09:121
09:13plus
09:141
09:140
09:15plus
09:151
09:17so
09:184
09:18has
09:183
09:19choices
09:191
09:201
09:211
09:221
09:221
09:221
09:221
09:231
09:231
09:231
09:231
09:231
09:231
09:231
09:281
09:292
09:321
09:331
09:331
09:342
09:352
09:352
09:352
09:352
09:371
09:391
09:402
09:451
09:461
09:472
09:482
09:482
09:482
09:482
09:481
09:501
09:512
09:511
09:511
09:521
09:522
09:53equal. Now what do you do? Go to right subtree. First of all, we will go to right subtree.
09:59Left diameter is zero, left height is zero. Right subtree is equal to zero, right height
10:05is equal to zero. What do you do? Calculate values for us. Values will come to this.
10:10One will be left diameter, one will be right diameter, one will be self diameter. Left diameter
10:16is zero, right diameter is zero. What will self become? Zero plus zero plus one is equal.
10:22So, here 1 will be final value. So, 5 will return 2 to 1 as its final diameter.
10:29So, 1 will be right diameter and right height is equal to 1. Now, what do you do?
10:352 will calculate your diameter. So, 2 will calculate your diameter. First, left diameter will be left.
10:43So, 2 will have one choice. Right diameter is equal to 2. Right diameter is equal to 5.
10:51So, this is the second choice is 1. Now, what do you do? Self diameter is 2 will be left
10:57height
10:57plus right height plus 1. Which is equal to 1 plus 1 plus 1. So, self diameter is equal to
11:052
11:20Now, what do you do? Left diameter is equal to 3. Which diameter is equal to 3? This diameter is
11:25equal to 3.
11:26Now, left subtree will return height with height. Height will be equal to 2.
11:33Now, 1 will be right subtree. Right subtree will be right subtree. Left subtree will be left subtree.
11:40Left subtree has left diameter return 0. Left height return 0. Now, right subtree will be right subtree.
12:11Left subtree has a choice that will be right subtree. Right subtree will be right subtree.
12:16Now, what is the choice of 3? That will be left subtree. Right subtree is equal to left subtree.
12:27तो यहां से 3 के पास जो right height आई थी that was equal to 1, यह थी मेरी right
12:31diameter, तो यहां पर value हो जाएगी self के तुरू diameter निकलने वाली 2, जो की यह वाली diameter होगी,
12:38तो यहां पर win करेगी यह 2 value, और 3 1 को return कर देगा 2 as its right diameter,
12:44और right height हो जाएगी 2 के बराबर, इन चीजों को मैं clear कर देती हूं, तो finally 1 के
12:50�
12:50पास क्या है, one के पास left की height आगई 2 के बराबर, left की diameter आगई 3 के बराबर,
12:55right की diameter आगई 2 के बराबर, right की height मेरे पास आगई 2 के बराबर, left में जो diameter
13:00हो कुछ ऐसी दिखती है, right में जो diameter हो कुछ ऐसी दिखती है, अब one calculate करेगा, सबसे पहले
13:06अपनी left diameter, already पता है हमें, left diameter one के लि�
13:132 ke beraber and self ki diameter hooghi left height plus right height plus 1. This is equal to maximum
13:22of 3, 2 and 5.
13:24To value meri paas ho jayegi 5. To is tree ki jho diameter meri paas nikal kar aegi that should
13:29be equal to 5.
13:30To mujhe recursively kya karna hai? Mujhe recursively har node ke liye uski diameter nikalni hai.
13:35Har node ke liye diameter nikalni ka matlab hai ki pehle assume karlo ki koji function hai diameter naam ka
13:41jho mujhe apni left diameter return kar dhega for my root dot left
13:45aur vohi function mujhe apni right diameter return kar dhega for my root dot right.
13:53Ab left height aur right height nikalni ke liye already mene kya kiya tha? Pahle sawal solve kar rakhha hai
13:58apna height function. Height function se kya hoota hai? Hemare kisi bhi tree ki ya sub tree ki height calculate
14:04hoke aajatii hai.
14:05Isko ma already pad chukye hain, lik chukye hain. To yaha pe mein paas kar dhungi root ka left toho
14:10mene left sub tree ki height aajayegi.
14:11Aur pass kar dhungi aapne root ka right. Toh kya hooga mene right sub tree ki height aajayegi.
14:16Ab jaisa hi kisi bhi node ke paas yeh 4 values aajatii hai. Yeh 4 values sabse zahadha important hai.
14:22Kisi bhi node ke paas agar meri yeh 4 values aagayi toho mene kisi bhi node par uski diameter calculate
14:26kar sakti ho.
14:27Kaisa calculate karungi? Sabse pahle toh self diameter nikaal loo. Self diameter ho jayegi left height plus right height plus
14:35one ke equal.
14:36Ek choice hai mere paas yeh, dousari choice hai left ki diameter, tisari choice hai right ki diameter. Mujhe kya
14:42karna hai? Inka maximum nikaal na hai.
14:44Maximum of my left diameter, my right diameter and my self diameter.
14:50In me se jo bhi maximum value aajegi usko hum return kar dheegi as our final diameter.
14:55Aur base case obviously humne dekhi liya dry run me base case tab ho ta hai jab meri paas null
14:59node hoti hai.
15:00Null node ki diameter bhi zero hai, uski height bhi zero hai.
15:03Toh jayse hi, hum kisi bhi aisi node par pahunc jayayin, jahaan par root ki value, mere paas null ke
15:08equal hai.
15:09Toh vahaan se meh directly return kar sakti ho, zero as my diameter. So this will be my base case.
15:15Toh is tariqe se teen possible diameter values ke bici meh, hum comparison karke maximum diameter ko calculate karta hai.
15:22Maynhe main function ke anndar kya ki hai? Is tree ko bana hai.
15:25Aur is tree ki bhi diameter mere paas ane wali hai five ke equal, jis meh include ho gi.
15:29Is level se koji bhi ek leaf node, is level ki dho no nodes and ek iya node.
15:32Toh value meri hooghi five ke equal.
15:34Am main function ke alawah already,
15:36Maynhe classroom class ke anndar,
15:38Maynhe ek static class bana rakhi hai node,
15:39Jis se meh tree ki nodes ko banaati ho.
15:41Aur, mere paas height function hai,
15:43Jho already exist karta hai,
15:44Jis ko am already pehle liek chukhe hai.
15:46Mujhe kya karna hai,
15:47Apeni diameter ko calculate karne ke liek function bana na hai.
15:50Toh, mein esko naam dhungi public static int diameter.
15:55Iska return type int hai,
15:56Kyunki har level per,
15:57Am apeni diameter ko calculate karne ke,
15:59Or isko phir return karne ke,
16:01As my return statement.
16:03Iske anndar single parameter ae ga,
16:04Which will be my root node.
16:07Sabse pahle base case liek lietay hai,
16:09Agar mere root ki value null ke equal hoi,
16:11Toh is case me,
16:11Mere diameter ki value bhi zero ke equal ho zaheegi.
16:14Har level per,
16:15Mujhe 4 values ko calculate karna hai.
16:17Mere left ki diameter,
16:18Mere left ki height,
16:20Mere right ki diameter,
16:21Mere right ki diameter,
16:24Mere right ki height.
16:26Is equal to,
16:28Is equal to,
16:28Diameter ko call laga duungi,
16:29Root ke left ke liek.
16:31Uske alawa my left height,
16:34Is equal to,
16:35Height ko call laga duungi,
16:37Apeni root ke left ke liek.
16:40And,
16:41My right height,
16:45Height ko call laga duungi,
16:47Root ke right ke liek.
16:49My right height,
16:51Is equal to,
16:52Height ko call laga duungi,
16:53Root ke right ke liek.
16:54Right ke liek.
16:55Jaisi mere paas,
16:564 values aajatia hai,
16:57Ab mujhe apni,
16:58Self diameter,
16:59Calculate karna hai hai,
17:00Yani khud ki diameter.
17:01That will be equal to,
17:03Left height,
17:05Plus right height,
17:07Plus right height,
17:08One.
17:09Ab joh bhi,
17:10Inka maximum value,
17:11Yani math dot max of,
17:14Pehle mein paas kar duung,
17:15Self diameter,
17:16Or,
17:16Math dot max of,
17:19Left diameter,
17:20And,
17:21Right diameter.
17:24Inka joh bhi maximum aayega,
17:26Usko mein kar duung ki,
17:27Apeni function se return.
17:28Yaha per,
17:29Shayad first time,
17:30Agar hum is tarikhe ka,
17:31Code liek rahe hai,
17:32Is tarikhe ki line,
17:32Dekh rahe hai,
17:33Toh is me kya hooga,
17:34Sab se pahle,
17:34Max function,
17:36Left or right,
17:37Maximum diameter,
17:38Nikaal leega,
17:38Phir joh bhi maximum,
17:40Nikal kar aayega,
17:41Usko compare,
17:42Apeni self diameter se,
17:43Or,
17:44Un dono me se,
17:45Mere paas,
17:45Maximum value,
17:46Nikal kar aayegi,
17:47Jisko mein return,
17:48Kar duung ki,
17:48As my final tree ki,
17:49Diameter,
17:50Toh ye hai,
17:51Mera diameter ke liye,
17:52Function,
17:52Isko in bar save karte hai,
17:54Apeni is tree ke liye,
17:56Main call,
17:56Apeni function ko,
17:58Diameter of,
17:59Root,
18:01Isko karte hai,
18:02Save and run,
18:03Toh mere paas,
18:04Jow tree ki diameter,
18:05That is equal to 5,
18:06Aabh ye jow code hai,
18:07Jow mein ne likhha hai,
18:09Isko calculate honne me,
18:10Diameter calculate honne me,
18:11Mere paas jow time complexity,
18:13Laghe gi,
18:13That will be,
18:13N square,
18:14N square q,
18:15Kyunki diameter ko calculate,
18:17Karne ke liye,
18:18Mere tree ki,
18:18Har ek node pa ja raha raha,
18:20Toh us node pa jaanen me,
18:21Mujhe jow time lag raha,
18:22Voh toh linear time hooga,
18:23Yarni end time,
18:52Par,
18:54Big of n,
18:55Mere paas time lagta hai,
18:56Thir mene is node pa bhi,
18:57Big of n time lagaya,
18:58Is node pa bhi,
18:59Big of n time lagaya,
19:00Is node pa bhi,
19:01Big of n time lagaya,
19:03Height ko calculate karne me,
19:04Toh is tariqe se,
19:05N nodes me,
19:06Agar mein big of n time laga raha,
19:08Toh meri time complexity,
19:09Ban jayegi,
19:10N square k equal,
19:11That is why,
19:11Yhe jow process hota hai,
19:12Diameter ko calculate karne ka,
19:14Jis me,
19:15Mujhe apni diameter,
19:16Or apni height ko,
19:17Alag-alag calculate karne nikala na pade ta hai,
19:19Alag-alag function se,
19:20Ye thoda sa,
19:21Mere paas,
19:21TDS process hota hai,
19:23Saat ke saat,
19:23Ye inefficient hai,
19:24Kyunki time complexity,
19:25Boh toh zyadha lagta hai,
19:27Isi liye,
19:27Hame ek second approach ko,
19:28Discuss kareenge,
19:29And,
19:29Is second approach ko,
19:31Hame sabse zyadha maximum times,
19:33Yarni,
19:33Hame jab bhi use karna hai,
19:34Diameter ke liye,
19:35Is second approach ko,
19:36Hame use karre hongge,
19:37Jis ko hum karrengge,
19:38Apne linear time me,
19:39Abh yaan pe kaafi sari students ka,
19:41Sawal ho sakta hai,
19:41Toh didi,
19:42Amne first approach ko,
19:43Kiyun use kiya,
19:44First approach is liye important hai,
19:45Kiyunki woh understanding,
19:47Build kerti hai,
19:47Second approach me to,
19:48Hame kya karrengge,
19:49First approach ko,
19:50Thoda sa or clean,
19:51Buna denge,
19:51Us ki or time complexity ko,
19:53Qum kar denge,
19:53Bas first approach se,
19:55Hame actually,
19:55Sama jata hai,
19:56Ki diameter recursively,
19:57Calculate hooti kaisi hai,