Java program to find the sum of all the nodes of a binary tree17 Mar 2025 | 2 min read In 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
a. calculateSum() will calculate the sum of nodes present in the binary tree:
Program:Output: Sum of all nodes of binary tree: 31 Next TopicJava Programs |
Java program to print the following pattern on the console To accomplish this task, we need to create two loops and the 2nd loop is to be executed according to the first loop. The first loop is responsible for printing the line breaks whereas the second...
1 min read
An array containing various numbers is given. The task is to create different groups, each containing only two elements, such that the difference between the group containing the largest sum and the group containing the lowest sum should be minimum. Note that any element can only...
5 min read
Write a code to prove that strings are immutable in java? Following program demonstrate it. File: ProveStringImmutable .java public class ProveStringImmutable { public static void referenceCheck(Object x, Object y) { if (x == y) { System.out.println("Both pointing to the same reference"); } else { System.out.println("Both are pointing to different reference"); } } public static void...
1 min read
Java Program to find Second Largest Number in an Array We can find the second largest number in an array in java by sorting the array and returning the 2nd largest number. Let's see the full example to find the second largest number in java array. public...
3 min read
The object is a basic building block of an OOPs language. In Java, we cannot execute any program without creating an object. There is various way to create an object in Java that we will discuss in this section, and also learn how to create...
7 min read
An array or list inArr of the numbers (either negative or positive) is given. The task is to find the number of smaller elements that exist on the right side of the current element. The following examples are given for a better understanding. Example 1: Input int inArr[] =...
14 min read
Java Program to find Smallest Number in an Array We can find the smallest element or number in an array in java by sorting the array and returning the 1st element. Let's see the full example to find the smallest number in java array. public class SmallestInArrayExample{ public static...
2 min read
Program to print the elements of an array in reverse order In this program, we need to print the elements of the array in reverse order that is; the last element should be displayed first, followed by second last element and so on. Above array in reversed...
2 min read
Java program to print the following pattern Algorithm: STEP 1: START STEP 2: SET lines=8 STEP 3: DEFINE i, j STEP 4: SET i=1.REPEAT STEP 5 to 14 UNTIL i STEP 5: SET j=1 STEP 6: REPEAT STEP 7 and 8 UNTIL j <=(lines/2) STEP 7: IF j is equals to...
3 min read
In this program, we need to check whether the given matrix is an identity matrix. Identity Matrix A matrix is said to be an identity matrix if it is a square matrix in which elements of principle diagonal are ones, and the rest of the elements are...
4 min read
We request you to subscribe our newsletter for upcoming updates.
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