# Find Level in a Binary Tree with Max Sum

Binary trees, with their simple yet significant styles, have found various applications in the DSA field and are also rapidly growing. They offer a hierarchical representation of the data that supports searching, sorting, and other things. Determining the maximum sum level in a binary tree poses an exciting challenge in various algorithms and design implementations.

It requires a vast knowledge of tree traversal techniques such as breadth-first and depth-first traversal. This article will mainly focus on the overview of binary trees and their properties, and it establishes a solid foundation for the discussions.

Now, we will look at some advantages of finding the maximum sum of the binary tree: -

1. One of the key advantages of finding the maximum sum of the binary tree is optimizing the performance; in certain applications, such as pathfinding algorithms and tree traversal algorithms, optimization of the performance can help a lot.
2. Next is the decision-making process in the tree structure; if there is a hierarchy of choices, then finding the maximum sum can emphasize the most optimal path in terms of choices.
3. It can also serve as a benchmark and prove to be very optimal when it comes to determining the algorithm analysis of the binary tree.

### Implementation

Output

A step-by-step explanation of the code

1. The code begins by declaring the necessary header files for the conduction of the operations.
2. The code will start by defining a structure for a binary tree called 'Node.' It has three members: a data value and a pointer to the left and right child.
3. Next, we declare a 'maxLevelSum' function that usually takes a pointer to the root of the binary tree as an argument and then returns an integer that represents the maximum sum of a tree.
4. We first check whether the base case is NULL or not, and if this is the case, then the tree appears empty.
5. The result variable is also initialized with the data value along with the root node value.
6. A queue will be used for the level order traversal of the binary tree.
7. We start a while loop that will continue until the 'q' is empty. Inside the queue, many things, such as the size of the queue, can be obtained.
8. Once the while loop is completed, the maximum level sum is found, and the result variable contains the answer.
9. Now, in the program's primary function, the binary tree is created by creating nodes using the 'newNode' function.
10. Finally, the program's primary function returns 0, indicating the successful execution of the program.

### Example 2)

Output

A step-by-step explanation of the code

1. The code begins by declaring the necessary header files for the conduction of the operations.
2. The code will start by defining a class named 'TPT' that contains the primary method and logic to calculate the maximum level sum.
3. Inside the 'TPT,' there is a nested class named 'node' with three members: a data value and a pointer to the left and right child.
4. Next, we declare a 'maxLevelSum' function that usually takes a pointer to the root of the binary tree as an argument and then returns an integer that represents the maximum sum of a tree.
5. We first check whether the base case is NULL or not, and if this is the case, then the tree appears empty.
6. The result variable is also initialized with the data value along with the root node value.
7. A queue will be used for the level order traversal of the binary tree.
8. We start a while loop that will continue until the 'q' is empty. Inside the queue, many things, such as the size of the queue, can be obtained.
9. Once the while loop is completed, the maximum level sum is found, and the result variable contains the answer.
10. Now, in the program's primary function, the binary tree is created by creating nodes using the 'newNode' function.
11. Finally, the primary function of the program returns 0, indicating the successful execution of the program.