Q. Program to find the sum of all the nodes of a Binary TreeExplanationIn this program, we need to calculate the sum of nodes present in the binary tree. First, we will traverse through the left sub-tree and calculate the sum of nodes present in the left sub-tree. Similarly, we calculate the sum of nodes present in the right sub-tree and calculate total sum by adding the root?s data. For the given tree, sum of nodes of the binary tree will be 1 + 2 + 5 + 8 + 6 + 9 = 31. Algorithm
SolutionPythonOutput: Sum of all nodes of binary tree: 31 COutput: Sum of all nodes of binary tree: 31 JAVAOutput: Sum of all nodes of binary tree: 31 C#Output: Sum of all nodes of binary tree: 31 PHPOutput: Sum of all nodes of binary tree: 31 Next TopicPrograms List |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India