✅ C++ ✅ Java ✅ Python || BFS || with Easy explanation ✅
Linkedin Profile (https://linkedin.com/in/ayush-kumar-a276a124a)\n//-------------> \uD83D\uDC7B Pls Upvote if it is helpful for You \uD83D\uDC7B <--------------
- Linkedin Profile (https://linkedin.com/in/ayush-kumar-a276a124a)\n//-------------> \uD83D\uDC7B Pls Upvote if it is helpful for You \uD83D\uDC7B <-----------------//\n# Approach\n- In this Question we have to Find that which level has maximum Sum among the all Level in the binary Tree\n- So we use **`BFS`**  traversal ( **`Level Order Traversal`** ) to solve the problem \n- In the level order traversal We iterate In The  binary tree level wise level \n-  `Initially we declare max_sum=INT_MIN`( to keep the track of maximum sum  among the all level )\n- `level_count=0;`  ( to keep the track of level )\n- `level_sum=0;`     (  to store the each level sum )\n- So we iterate each level and compute **`Level_sum`** of each level and compare to **`max_sum`** then if **`max_sum`** less than the **`level_sum`** so we update the \n- `max_sum=level_sum`   \n- and also Update the `samllest_level=level_count`\n- Pls see the code for better understaing \n# Complexity\n- Time complexity:----> **O(N)**\n\n- Space complexity: -----> **O(N)**\n- \n```C++ []\nclass Solution {\npublic:\n    int maxLevelSum(TreeNode* root) {\n\n        std::queue<TreeNode*> q;\n        q.push(root);\n        int max_sum = INT_MIN, level_count = 0, smallest_level = 0;\n        \n        while (!q.empty()) {\n            int level_size = q.size();\n            int level_sum = 0;\n            \n            for (int i = 0; i < level_size; i++) {\n                auto currNode = q.front();\n                q.pop();\n                \n                level_sum += currNode->val;\n                \n                if (currNode->left) {\n                    q.push(currNode->left);\n                }\n                \n                if (currNode->right) {\n                    q.push(currNode->right);\n                }\n            }\n            \n            level_count++;\n            \n            if (max_sum < level_sum) {\n                max_sum = level_sum;\n                smallest_level = level_count;\n            }\n        }\n        \n        return smallest_level;\n    }\n};\n```\n```JAVA []\nclass Solution {\n    public int maxLevelSum(TreeNode root) {\n        Queue<TreeNode> queue = new LinkedList<>();\n        queue.add(root);\n        int maxSum = Integer.MIN_VALUE, levelCount = 0, smallest_level = 0;\n\n        while (!queue.isEmpty()) {\n            int levelSize = queue.size();\n            int levelSum = 0;\n\n            for (int i = 0; i < levelSize; i++) {\n                TreeNode currNode = queue.poll();\n\n                levelSum += currNode.val;\n\n                if (currNode.left != null) {\n                    queue.add(currNode.left);\n                }\n\n                if (currNode.right != null) {\n                    queue.add(currNode.right);\n                }\n            }\n\n            levelCount++;\n\n            if (levelSum > maxSum) {\n                maxSum = levelSum;\n                smallest_level = levelCount;\n            }\n        }\n\n        return smallest_level;\n    }\n}\n```\n```Python []\nclass Solution:\n    def maxLevelSum(self, root):\n        queue = deque()\n        queue.append(root)\n        max_sum = float(\'-inf\')\n        level_count = 0\n        smallest_level = 0\n\n        while queue:\n            level_size = len(queue)\n            level_sum = 0\n\n            for _ in range(level_size):\n                currNode = queue.popleft()\n\n                level_sum += currNode.val\n\n                if currNode.left:\n                    queue.append(currNode.left)\n\n                if currNode.right:\n                    queue.append(currNode.right)\n\n            level_count += 1\n\n            if level_sum > max_sum:\n                max_sum = level_sum\n                smallest_level = level_count\n\n        return smallest_level\n```\n\uD83D\uDC7B IF YOU LIKE THE SOLUTION THEN PLEASE UPVOTE MY SOLUTION BECAUSE IT GIVES ME MOTIVATION TO REGULARLY POST THE SOLUTION\uD83D\uDC7B*\n![image.png](https://assets.leetcode.com/users/images/f978ce00-bd16-442f-a7c0-408b2f01bba0_1686792712.588753.png)\n\n
['Breadth-First Search', 'Python', 'C++', 'Java']