• 2 days ago
Transcript
00:00Next, when we talk about this question, it's called count of nodes.
00:02I have to count the total number of nodes in my binary tree.
00:07If I talk about count, then I have how many nodes in this binary tree?
00:111, 2, 3, 4, 5, 6.
00:14So, I have the final count return, that should be equal to 6.
00:19Now, in the last question, we talked about height,
00:21that in the trees, there are mostly questions that can solve in recursive fashion.
00:25And the process is usually the same, that recursively, we calculate for child and then we automatically calculate our parents.
00:34So, in that same recursive process, we will calculate it.
00:38I will try to go to all the children.
00:41So, 1 will go to your left child, then 2 will go to your left child, then 4 will go
00:47to your left child, then 4 will go to your left child.
00:48Now, let's take a look at any other way, first of all, my left subtree gave me my left count
00:55return.
00:58I will create a count function, which I will pass the root of left.
01:02And this count function, I have faith that this count function automatically gave me the left count return.
01:09Then, what I did, I called the same function for my right count.
01:13So, it has taken my root.right and gave me the count for my right count.
01:20Now, if I have my left subtree and my right subtree,
01:26I have this count and this count,
01:29I can calculate my whole tree count.
01:31How?
01:33That will happen.
01:34Left count plus right count plus one.
01:37Because I have all the left subtree notes,
01:39I have counted the right subtree.
01:42Now, if I get the sum of them,
01:43then I have all the subtree notes count.
01:46So, I have to add one.
01:48Why?
01:48Because I want to include myself in my notes notes.
01:53So, if I have left count and right count,
01:56then my count,
01:58or I call myself count,
01:59or I call tree count,
02:01I call tree count,
02:02tree count,
02:04that should be equal to left count,
02:06plus right count,
02:08plus one.
02:09So, we can return this value.
02:11And if I try to think of this recursive function,
02:12I will try to think of base case.
02:14In base case,
02:15it is possible that my tree is completely empty.
02:17I have simply null nodes.
02:19So, what can I say?
02:20If there is no node in tree,
02:22there is no node.
02:23So, if there is no node,
02:26my root becomes null.
02:29In this case,
02:30I have to return me to 0.
02:33Because my tree count will be equal to 0.
02:36One time, let's try to dry run this tree.
02:38In this tree,
02:39first of all,
02:40we will go to our root node,
02:41which is 1.
02:42So, 1 will try to calculate your left subtree count.
02:45Then, we will go to 2.
02:472 will become root.
02:502 will become root.
02:502 will try to calculate your left subtree count.
02:53Then, 2 will go to 4.
02:544 will become root.
02:564 will become root.
02:57What will try to do?
02:58Your left subtree count will be calculate.
03:00So, 4 will go to your left subtree.
03:02Which is a null node.
03:03What will do?
03:03Null will be null.
03:04What will happen?
03:04Null will be null.
03:05We will know that 0 will be equal to 0.
03:06So, null will return 0.
03:08Then, 4 will go to right subtree.
03:10Right subtree will return 0.
03:11So, what will be 4 count?
03:134 will count.
03:14Left count plus right count plus 1 is equal.
03:17Which is equal to 0 plus 0 plus 1.
03:20That is equal to 1.
03:21Now, 4 will return 2 to 1 as left subtree count.
03:26Now, 2 will try to calculate your right subtree count.
03:29So, he will ask 5.
03:315 will do that.
03:32Left subtree count will be null node return 0.
03:35Then, right subtree count will be null node return 0.
03:38So, 5 will return 2 to 0 plus 0 plus 1.
03:42Which is equal to 1.
03:482 will return 3 to 5.
03:540 will return 3 to 100 plus 0.
03:54Now, 1 will go into right subtree count and calculate 3 to 3.
03:58Then, straight subtree count will be null value.
04:032 will return 3 to 3?
04:07Then, 1 will count on 3 to 4.
04:07Now, 2 will revert 3 to 3 about.
04:071 will return 3 to 3 better.
04:073 will go into left subtree and calculate 3 to the count right subtree.
04:11Value WILL return null value 0 from left subtree that will return 0.
04:12The $2 return value is 0.
04:13that will be equal to 1, 6 return 3 to 1, 3 ke paas left sub tree se aya 0,
04:19right sub tree se aya 1,
04:20to 3 ka count kya ho jayega, 1 plus 1, to 3 1 ko return kardega 2,
04:26matlab 1 ke left sub tree ke anndar 3 nodes hain and 1 ke right sub tree ke anndar 2
04:31nodes hain,
04:31to 1 total jab tree ka count calculate karega, to who kaise karega, it will be 3 plus 2 plus
04:371,
04:38to 1 jo count return karega as final answer, that will be equal to 6, to hum count ko print
04:44karega as 6,
04:45or count ko calculate karte time b, aapne notice kiya ho ga ki hum har node per ek baar gai,
04:50jab bhi hummare tree ke anndar n nodes hain and hum har node per ek baar jate hain,
04:55uska matlab hai ki humme linear time laga ra hai recursively aapne tree ka count calculate
04:59karne mein, to time complexity ho jayegi big of n, already minne main function ke anndar same tree ko construct
05:05kiya hoa hai,
05:06aapni static node class me ne bana hi hui hai, ab me kya karte hum,
05:10aapne tree ki count calculate karne ke liha,
05:12mein ek function bana nane waali hu, public static int count,
05:16iske anndar single parameter ho ga, which will be my root,
05:19ab sabse phile base case ligh leetay hain, agar root kisi bhi point per null ho jati hain,
05:23yaa mire paas empty tree haata hai, to us case mein,
05:27mujhe return karna hai count ko as zero,
05:31baaki cases mein, sabse pahle to me left ka count calculate karlo,
05:35left count ho jayegah count of root dot left,
05:39thir mein right ka count calculate karlo, right count is equal to count of root dot right,
05:46uske baad mujhe kya karna hai,
05:47meri khud ka count ho ga my left count plus right count plus one,
05:51toh mein yaas se return kar dunggi, my left count plus my right count plus one,
05:58isko kar lietay hain safe,
06:00is tree ke liha, meri paas jo count return ho kar anna chahiye that should be equal to six,
06:04toh mein call karne wali hu, apne count function ko,
06:10is tree ke anndar meri paas nodes joh hai seven ke equal hain,
06:14joh humne dry run kiya tha,
06:16uske anndar ye six wala node exist nahi karta tha,
06:18is tree ke anndar seven hai,
06:19isi liye meri paas answer me return ho kar aya hai seven,