📜  C ++ |其他C ++ |问题6(1)

📅  最后修改于: 2023-12-03 14:59:36.576000             🧑  作者: Mango

C++ | Other C++ | Question 6

Introduction

Welcome to the sixth question of our C++ problem series! In this question, we will test your knowledge of C++ programming and problem-solving skills. You will be given a problem statement and will have to write a solution for it using C++ language.

Problem Statement

Given a binary tree, write a program to find the level that has the maximum sum of nodes.

For example, consider the following binary tree:

      1
    /   \
   2     3
  / \     \
 4   5     6

In this case, the maximum sum level is level 2, which has a sum of 11.

Solution

We can solve this problem using Breadth First Search (BFS) algorithm. We will first traverse the tree breadth-wise, level by level, and calculate the sum of each level. Then we will keep track of the maximum sum and the level at which it occurs.

Here is how we can implement this solution in C++:

// Definition for a binary tree node.
struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};

class Solution {
public:
    int maxLevelSum(TreeNode* root) {
        queue<TreeNode*> q;
        q.push(root);
        int level = 1, maxLevel = 1, maxSum = INT_MIN;
        
        while (!q.empty()) {
            int size = q.size();
            int sum = 0;
            
            for (int i = 0; i < size; i++) {
                TreeNode* node = q.front(); q.pop();
                sum += node->val;
                
                if (node->left) q.push(node->left);
                if (node->right) q.push(node->right);
            }
            
            if (sum > maxSum) {
                maxSum = sum;
                maxLevel = level;
            }
            
            level++;
        }
        
        return maxLevel;
    }
};
Explanation

We start by creating a queue and pushing the root node into it. We also initialize three variables: level, maxLevel, and maxSum. level keeps track of the current level we are at, maxLevel keeps track of the level with the maximum sum, and maxSum keeps track of the maximum sum so far (initialized to a minimum value using INT_MIN).

We then start the BFS loop. In each iteration of the loop, we find the size of the queue and initialize a variable sum to 0. We then loop through all the nodes at the current level and add their values to the sum. If the node has left and/or right child, we add them to the queue.

After we have processed all the nodes at the current level, we compare the sum with maxSum. If sum is greater than maxSum, we update maxSum and maxLevel.

At the end of each iteration, we increment the level variable.

Once the BFS loop is complete, we return maxLevel.

Conclusion

In this question, we have seen how to find the level with the maximum sum in a binary tree using BFS algorithm. We hope you found this question and solution informative and useful! Stay tuned for more C++ problems and solutions!