• 12 hours ago
Transcript
00:00Next question which we will talk about is transform to a sum tree.
00:04I want to convert some tree to some tree to some tree.
00:08Sum tree to say that first if there was a node that had a left sub tree and it had
00:17a right sub tree,
00:19and the node in it had a data.
00:22So now this node will transform in this way,
00:25where the data will be left sub tree to the total data plus my right sub tree to the total
00:31data.
00:32So here we will remove the data from the left sub tree to the total data,
00:36and the right sub tree to the total values.
00:38And we will remove all of them,
00:39and we will replace all of our node's original data.
00:44So I can say that within each node it will be the sum of left and right sub trees.
00:49So if I start from leaf level,
00:52in leaf level 4,
00:54left sub tree and right sub tree will be 0,
00:57because the left node of the two values are null.
01:00So here 4 will be 0.
01:025 will be a leaf node,
01:04left and right will be 0.
01:066 will be 0,
01:07left and right will be 0.
01:097 will be 0.
01:11If I talk about 2,
01:13left sub tree is 4, right sub tree is 5,
01:16both sum is 9.
01:17So here 2 will remove its value in our learning values.
01:21so for two,
01:25so for 1 a central variable,
01:27Soards The value of 2 we will remove here,
01:28we will remove its values and here Nicholas,
01:30elevates the sum tree to the 4 plus 5 plus 2.
01:33This is 11
01:34and that the right sub tree will also shows 1
01:36rather than 2.
01:38So,
01:39We continue to improve its value,
01:42equal to 27. So, in this way, we are going to transform a sum tree in this way. And it
01:48will be a simple code that we used to calculate the sum in the beginning of our tree.
01:53This is an easy level question. Try to pause the video right now and solve this question. In fact, the
02:00maximum students will write this code themselves.
02:03Because this is a very easy question. If we look at the values, we can see some confusing values.
02:08If we talk about this approach, then the approach will be like this, that what I will do for every
02:15node?
02:16First, I will remove the sum of its left subtree and the right subtree.
02:24Now, I will remove the sum, left subtree and the right subtree. Then, it will set some data variables within
02:32a particular data.
02:33That means the root of the data is becoming the first c-equal community. And the root of the data
02:37will change the data.
02:39I will then give the sum of my left sum and right sum. And I will check the sum of
02:45our data.
02:46So, what will the отлич of the number of the value be?
02:48change your value and send your original data. If the root value is null, what do I have to do
02:57here?
02:57I have to return 0 from here. So, I consider the null data as 0. So, as we come to
03:021, what do we do?
03:041 will try to remove the sum of left subtree, this will also try to remove the sum of left
03:07subtree,
03:09this will also come to 0. What do we do? It will return 0 to null node.
03:15And the right side of null node will return 0 value. 4 will be left and right.
03:22So, 4 will transform itself and change your value to 0 and return itself.
03:29Here, 4 will return 4 to your value.
03:33I will say that 2 will be left subtree to the sum of the value, which will return the value.
03:39Here, we have a little variation in our approach.
03:42The return will actually be my left child's value and my right child's value.
03:49Like, 4 will return my value.
03:52So, I will return my value to my value.
03:54So, every time in return, we return our self's value.
03:56If I draw my value's value and I shall return it to my root.
03:58so, they will return the same way to the recursive function.
04:02If I consider function, the function pouvoir number of the root
04:17return, right sub tree will transform in some tree and right child will return the value
04:23then data value will store my root data, root data will become actually my left sub tree
04:31value plus my right sub tree, what I can say, left sub tree will become root left plus my left
04:42child
04:43and my right sub tree will become root right data plus my right child
04:52because from left child all nodes are already stored in my root left child
04:58and the root of the right nodes are stored in my right child
05:02and the root of the right nodes are stored in my right child
05:04so that will become my final root data and I will return my data
05:10so basically code is this way to work here
05:12if 4 returned 4, meaning its value returned
05:16then we will go to 5, 5 will go to null and null
05:20null will return 5 to 0, this will also return 0
05:24so 5 will return 5 to 0, so 5 will return 5 to 0
05:27and will return 5 to 0 as 5
05:31now if I talk about 2, what will happen to 2
05:33what will happen to 2?
05:34we will first observe how our code is doing
05:372 has left child's value value is 4
05:40right child's value is 5
05:43so 2 will become the data
05:45first we will store the data in 2
05:48and this root of the data will be
05:51root of the left root of the left value
05:53this value will be 0
05:54and this value will be 0
05:56that is equal to 0
05:57plus its left child 4
05:58plus its root of the right child
06:01which is equal to 0
06:01plus its right child which is equal to 5
06:03this value will become 9
06:05so here we will replace 2
06:06and we will write 9
06:08and 2 will return value as 2
06:112 what made it to be
06:13now what turn the root on the left
06:14which is the value of the right
06:14which is the new value
06:15and the previous value went
06:194 values add around
06:20this value had allocated 9
06:21and the 4 values were replaced
06:22which replaced by its own data
06:23and itsamat телеф saints
06:25and its original data which is equal to 2
06:26return its parent to�� organizes
06:32which is equal to 2
06:393B, Right, Left Subtree
06:40Going to Call
06:416 what ?
06:42login to our value,
06:43store it
06:43And for
06:44return,
06:466
06:46and what
06:48the value your
07:08plus 9 value of right child 0
07:10plus purani value which is 7
07:12ये बन जाएगा 13 के equal
07:14तो 3 की जगे हम store करवा लेंगे 13 को
07:16और 3 क्या करेगा ऊपर वाले level को
07:18अपनी value purani value return करेगा
07:20यानि 3 return करेगा
07:22तो इसको मैं हटा देती हूँ for clarity
07:24अब बात करते हैं अपनी root node की
07:261 के पास क्या हुआ
07:28left से 2 आया right से 3 आया
07:30और नई updated value है 9 and 13
07:32तो 1 क्या करेगा
07:34इसके left child की value 2 के equal आगे है
07:36right child की value 3 के equal आगे है
07:38अपना data तो store करवा लेगा 1 कहीं और
07:40और root के data को change करके कर देगा
07:42root का left यानि
07:44नई value plus purani value
07:46which is equal to 2 plus नई value
07:48which is equal to 13 plus purani value
07:50which is equal to 3
07:51this becomes 11 plus 16 which is equal to 27
07:55so 1 अपनी value को change करके कर लेगा
07:5727 के equal
07:59तो अब जहां से देखे तो ये मेरा पूरा एक sum tree बन गया है
08:02जिसमें ऊपर 27 है
08:03फिर left में 9 है right में 13 है
08:06और मेरे सारे के सारे leaf notes
08:07transform हो गया है to values 0
08:09इसी के लिए इसी same pseudo code को
08:12अब हम code में convert करने वाले
08:13और ये जो proper code रहेगा
08:15ये हमारा linear time में चलेगा
08:17which will be big of n
08:18main function के अंदर already मैंने अपने tree को बनाया हुआ है
08:21और इसके लिए मेरे पास कुछ ऐसा expected tree आना चाहिए
08:24मैं क्या करने वाली हूँ
08:25एक function लिखने वाली हूँ
08:27public, static, void type का होगा
08:29मतलब कुछ return नहीं होगा
08:31उसी node के अंदर सारे transformation हो जाएंगे
08:33इसको मैं नाम दूँगी transform
08:35इसके अंदर पास करूँगी अपनी root node
08:38मुझे सबसे पहले क्या करना है
08:41सबसे पहले तो मैं call लगा दूँ
08:42अपने left child को transform करने के लिए
08:45तो मैं transform को call लगाऊंगी
08:47for root का left
08:48और यहां से जो भी value return होकर आएगी
08:50उसको मैं store कर लाऊंगी
08:51in my left child variable
08:55फिर मैं call लगाऊंगी
08:57for right child
08:58इसका return type हम integer रख लेते हैं
09:01ताकि अपनी original values को यह पास कर पाएं
09:03और transform को call लगा दूँगी
09:06for root का right
09:08फिर मुझे क्या करना है
09:10किसी data variable के अंदर store कर लूँ
09:11अपनी root का data, जिसको मैं return करने
09:14वाली हूं last में. last में हैं
09:15अपनी data को return करने वाली हूं, पर return
09:17करने से पहले मुझे अपनी value को change करना है
09:20यानि root का जो data है
09:21that becomes equal to
09:23अब मेरे root का left का data है
09:26plus my left child
09:28यह हो गई मेरी next
09:29left की value, यह हो गई मेरी
09:31पुरानी left की value, plus
09:33my root ke right ka data
09:37plus my
09:38right child.
09:39This is my root ke right
09:42value. This is my root ke right
09:44ke right ke purani value.
09:45In 4 values ko add
09:47karke, I have to add all the
09:48subtree sum to my root ke data
09:50store and return to my data.
09:53Base case, here it is, if
09:55root ke value is null to any point
09:56to null, then I will return
09:58to 0. So, this is the leaf
10:01data that will ultimately
10:020. This is my transform tree.
10:06We have a pre-order function
10:08to write a pre-order function, so
10:09we will print our tree and
10:11check values. I will return type
10:14to give you void. Pre-order
10:15we have to write a lot of times.
10:16Node root
10:20if root is equal to null,
10:22then we will return
10:24to pre-order. No, then
10:26pre-order we will print
10:27our root data
10:32after call
10:33we will return to pre-order for root
10:35ka left and then
10:37my pre-order for
10:39root ka right.
10:41This is my pre-order function,
10:43this is my pre-order function,
10:44this is my transform function.
10:45main function
10:46main function
10:46se kya call
10:47karoongi?
10:47Pahle to
10:48transform function ko
10:49call
10:49lagaungi
10:49apne root ke liye.
10:50Uske baad
10:51nai updated root
10:52hooghi
10:53uske liye
10:53mein pre-order
10:54ko call
10:54karoongi.
10:55Isko
10:56kar lietai
10:56safe
10:57unrun.
10:59Pointer exception
11:00aya hai
11:00for 294.
11:02Line
11:02number 294
11:03per
11:05mujhe
11:06apne root
11:06ke left
11:07ka
11:07data
11:07tbhi
11:08milega
11:08jab
11:08mire root
11:09ka
11:09left
11:09null
11:10ke
11:10equal
11:10na
11:11ho
11:11to
11:11h
11:12m
11:12here
11:12s
11:14abse
11:14pahle
11:15isko
11:15thoda
11:15sa halka
11:16sa change
11:16kar
11:16dhe
11:16t
11:17total
11:19left
11:20ka
11:20data
11:20that
11:21becomes
11:22equal
11:22isko
11:23main
11:23thoda
11:24sa
11:24change
11:24kar
11:24dhe
11:24t
11:25int
11:26of
11:28new
11:29left
11:29ka
11:30data
11:31agar
11:32root
11:32ka
11:32left
11:33null
11:33ke
11:33equal
11:34ho
11:34jata
11:34hai
11:34tb
11:35to
11:35value
11:35zero
11:36ho
11:36jai
11:36nahin
11:36to
11:36value
11:37ho
11:37jai
11:37root
11:38ke
11:38left
11:39ke
11:39data
11:40ke
11:40equal
11:43to
11:43yaha
11:43me
11:43licks
11:44sakti
11:44yun
11:44apna
11:44new
11:45left
11:45new
11:46left
11:46ki
11:46value
11:47agar
11:47root
11:47ka
11:48left
11:48null
11:48hai
11:48to
11:49null
11:49nahin
11:49to
11:51new
11:52left
11:52ki
11:52value
11:52agar
11:53root
11:53ka
11:53null
11:53hai
11:54to
11:54zero
11:54ho
11:54jai
11:55ghi
11:55nahin
11:55to
11:55yi
11:55ho
11:55jai
11:55ghi
11:55root
11:56ke
11:56left
11:56ka
11:56data
11:57isi
11:57tariqe
11:58se
11:58mera
11:58new
11:59right
11:59hai
12:00uski
12:00value
12:01agar
12:01root
12:01ka
12:01right
12:04null
12:05ke
12:05equal
12:05hai
12:05to
12:06us
12:06case
12:06me
12:06ho
12:06jai
12:06ghi
12:07zero
12:07nahin
12:07to
12:07yi
12:07ho
12:07jai
12:07ghi
12:08root
12:08ke
12:08right
12:09ka
12:11data
12:12here
12:13me
12:13lick
12:13dhung
12:13ghi
12:13apna
12:14new
12:15right
12:17hai
12:17thoda
12:18transformation
12:23node
12:23ke
12:23liye
12:24new
12:24left
12:25and
12:25new
12:26right
12:26ho
12:26ga
12:27already
12:28null
12:28ke
12:28equal
12:28to
12:29iska
12:29data
12:29nai
12:29nik
12:30kale
12:30pahe
12:30ghe
12:30to
12:30us
12:30case
12:30me
12:30ham
12:31inki
12:31values
12:31ko
12:31zero
12:32consider
12:32kare
12:32kare
12:33iskko
12:33save
12:34kare
12:34kare
12:34lehet
12:34hai
12:34dhubara
12:34run
12:35kare
12:35kare
12:35kare
12:37kare
12:37pre
12:38order
12:38sequence
12:38nikal
12:39kare
12:39aya
12:39expected
12:40sum
12:41tree
12:41kuch
12:41aisa
12:42bana
12:42hoga
12:42jis
12:5327
12:53ke
12:54right
12:54subtree
12:55me
12:55aya
12:55to
12:5513
12:55mere
12:56paas
12:56print
12:56hua
12:56hai
12:5613
12:57ka
12:57left
12:57and
12:58right
12:58child
12:58which
12:58is
12:59equal
12:59to
12:59zero
12:59and
12:59zero
13:00to
13:00kuch
13:00is
13:00is
13:00tariqe
13:01se
13:01hama
13:01aipne
13:01tree
13:01ko
13:02transform
13:02kar
13:02sakte
13:03hai
13:03to
13:03a
13:03sum
13:04tree