00:00Next question which we are going to talk about is its name sum of nodes.
00:03What do I have to do?
00:04I have to calculate the sum of nodes to my binary tree.
00:09So how will I do it?
00:11In this level, it will be sum 1.
00:12Here it will come.
00:13I have 5 equals here.
00:15Here it will come.
00:155 plus 6 is 11, 15.
00:18This is equal to 21.
00:20So I have the final sum that should be equal to 21.
00:23Now we have multiple ways to do this question.
00:25What can you do?
00:27Level order traversal.
00:30I can calculate the sum of nodes.
00:32But it is a small and easier version.
00:35Recursive form.
00:37Recursively, what do I do?
00:39Recursively, I go to every node.
00:40If I have my left child to calculate the sum of nodes,
00:43and my right child to calculate the sum of nodes,
00:46then I can calculate the sum of nodes.
00:48I can calculate the whole tree to calculate the sum of nodes.
00:51If I consider this root node,
00:53if I consider this root node,
00:56I will put the whole left sub-tree here.
00:57I will return this sum to the sum of nodes.
00:59I will return this sum to our sum.
01:00I will return this sum to 11.
01:00Let's take a recursive function.
01:02It will return this sum.
01:04How did it return this sum?
01:06I can write left sum is equal to.
01:08I have called a sum function,
01:10which I have passed root.left.
01:12So, I have the whole left sub-tree sum.
01:14So, I have the whole right sub-tree sum,
01:17which is equal to 9.
01:19So, I have the function called sum,
01:21which I have passed root.right.
01:24So, I have returned this sum to my right sum.
01:28So, what can I say?
01:29I can remove the sum of root of my root.
01:31I mean, how can I remove the sum of this whole tree?
01:34If one is my root,
01:35I will add my left sub-tree sum to my right sub-tree sum.
01:39I mean, my sub-tree sum will complete my sum to this.
01:42Plus, I will add my node value.
01:45So, in this way,
01:46I can remove my whole sub-tree sum.
01:48Which will be equal to 11 plus 9 plus 1.
01:52This value will be 21.
01:53So, my self-sum or my tree sum will be equal to my left sum,
02:02plus my right sum,
02:04plus my root data.
02:08And here, if my tree is empty,
02:10in the base case,
02:11if my tree is empty,
02:13what do we need to do?
02:15If my root value is null to equal,
02:20then my sum will be equal to 0.
02:22Once we dry run it,
02:24on our tree,
02:25what do I need to do recursively?
02:27I need to do all the notes.
02:28I need to calculate the sum.
02:29So, first of all,
02:30I will come from 1 to 2,
02:31and 2 will come from 4.
02:32For 4,
02:33left sub-tree or left child
02:35will return 0.
02:37Because it is null.
02:37Right child will return 0.
02:39What will return your parent to your parent?
02:414 will return 4 to your parent.
02:44Because sum will be equal to 0,
02:46plus 0, plus 4.
02:48Value comes from 4.
02:49Now,
02:492 will call for right sub-tree.
02:51So,
02:515 will come from 5.
02:525 will give left child 0.
02:53Right child 0.
02:545 will return 5 equal.
02:57What will do?
02:582 will add 4 and 5,
03:00which is 9.
03:01And then,
03:01it will add itself to its value,
03:03which is 11.
03:03So,
03:042 will return value as 11.
03:07Now,
03:081 will have to calculate the sum of left sub-tree.
03:10Which is equal to 11.
03:12Now,
03:121 will have to calculate the sum of right sub-tree.
03:15So,
03:15the call will be added to 3.
03:173 will return 0.
03:19Because there is null.
03:203 will return 0 to the right sub-tree.
03:22First,
03:23we will go to 6.
03:23Then,
03:23left sub-tree will return 0.
03:25Right sub-tree will return 0.
03:276 will return 0.
03:29Then,
03:30what will 3 do?
03:313 will add 0 and 6.
03:33And,
03:33it will add value.
03:35So,
03:353 will return 9.
03:371 will return 0.
03:38Left sub-tree is 11.
03:40And,
03:40right sub-tree is 9.
03:41So,
03:41what will 1 do?
03:4211 will add 9,
03:43which is equal to 20.
03:45And,
03:45you will add your value,
03:46which is equal to 21.
03:48So,
03:48I will have the final sum of nodes.
03:50That will be equal to 21.
03:52And,
03:52in this whole process,
03:53we have to go to every node.
03:55We have to go to one single node.
03:56That's why,
03:57if my node has n nodes in my tree,
03:59then,
03:59my time complexity is a linear time complexity.
04:02Which is equal to big of n.
04:04I have made my main function.
04:05I have already created a tree.
04:07This tree is a 7 value node.
04:10So,
04:10my sum is expected here.
04:12It will be 28.
04:13So,
04:14what will I do?
04:14I will call a function.
04:15Public static int sum.
04:18The sum will be my root.
04:22In the base case,
04:23I will check if my root is equal to null.
04:24equal to equal.
04:25So,
04:25in this case,
04:25I will return to 0.
04:32First,
04:33I will calculate my sum of root.left.
04:41Then,
04:42I will calculate my right sub-tree.
04:44which will be equal to sum of root.right.
04:48My sum will be my left sum,
04:52plus my right sum,
04:54plus my root data.
04:58So,
04:59I will return this value to my function.
05:01So,
05:02a small function,
05:02it will be your sum to calculate your recursive function.
05:05In the recursive function,
05:05it will be very easily
05:06we can calculate all the notes on the sum.
05:10So,
05:10we will print sum of root.
05:13Let's save.
05:15And now run.
05:16So,
05:17the sum of root data is equal to 28.
05:20That is equal to 28.