C++ and Python3 || BFS || Simple and Optimal || Beats 100%
\n\n# Code\nC++ []\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n *     int val;\n *     TreeNode *left;\n *     TreeNode *right;\n *     T
> ![image.png](https://assets.leetcode.com/users/images/5ef27965-2fc3-4e0c-aafc-c3033fd30158_1709358829.1378493.png)\n\n# Code\n```C++ []\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n *     int val;\n *     TreeNode *left;\n *     TreeNode *right;\n *     TreeNode() : val(0), left(nullptr), right(nullptr) {}\n *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n    int maxLevelSum(TreeNode* root) {\n        if(!root) return 0;\n        queue<TreeNode*> q;\n        q.push(root);\n        int maxSum = INT_MIN;\n        int level = 0;\n        int maxLevel = 0;\n        while(!q.empty()) {\n            int size = q.size();\n            int currSum = 0;\n            while(size--) {\n                TreeNode* node = q.front();\n                q.pop();\n                currSum += node->val;\n                if(node->left) q.push(node->left);\n                if(node->right) q.push(node->right);\n            }\n            level++;\n            if(currSum > maxSum) {\n                maxSum = currSum;\n                maxLevel = level;\n            }\n        }\n        return maxLevel;\n    }\n};\n```\n```Python3 []\n# Definition for a binary tree node.\n# class TreeNode:\n#     def __init__(self, val=0, left=None, right=None):\n#         self.val = val\n#         self.left = left\n#         self.right = right\nclass Solution:\n    def maxLevelSum(self, root: Optional[TreeNode]) -> int:\n        if not root:\n            return 0\n        q = deque()\n        q.append(root)\n        maxSum = float(\'-inf\')\n        level = 0\n        maxLevel = 0\n        while q:\n            size = len(q)\n            currSum = 0\n            for _ in range(size):\n                node = q.popleft()\n                currSum += node.val\n                if node.left:\n                    q.append(node.left)\n                if node.right:\n                    q.append(node.right)\n            level += 1\n            if currSum > maxSum:\n                maxSum = currSum\n                maxLevel = level\n        return maxLevel\n```
['Tree', 'Breadth-First Search', 'Queue', 'Binary Tree', 'C++', 'Python3']