Python🔥Java🔥C++🔥Simple Solution🔥Easy to Understand
An Upvote will be encouraging \uD83D\uDC4D\n\n\n# Video Solution\n\n# Search \uD83D\uDC49Maximum Level Sum of a Binary Tree By Tech Wired\n# or\n# Click the Lin
# An Upvote will be encouraging \uD83D\uDC4D\n\n\n# Video Solution\n\n# Search \uD83D\uDC49` Maximum Level Sum of a Binary Tree By Tech Wired`\n# or\n# Click the Link in my Profile\n\n\n```Python []\n\nclass Solution:\n    def maxLevelSum(self, root):\n        if not root:\n            return 0\n\n        queue = [root]\n        max_level = 1\n        max_sum = float(\'-inf\')\n        level = 1\n\n        while queue:\n            level_sum = 0\n            next_level = []\n\n            for node in queue:\n                level_sum += node.val\n\n                if node.left:\n                    next_level.append(node.left)\n                if node.right:\n                    next_level.append(node.right)\n\n            if level_sum > max_sum:\n                max_sum = level_sum\n                max_level = level\n\n            queue = next_level\n            level += 1\n\n        return max_level\n\n```\n```Java []\n\nclass TreeNode {\n    int val;\n    TreeNode left;\n    TreeNode right;\n\n    TreeNode(int val) {\n        this.val = val;\n        this.left = null;\n        this.right = null;\n    }\n}\n\nclass Solution {\n    public int maxLevelSum(TreeNode root) {\n        if (root == null) {\n            return 0;\n        }\n\n        Queue<TreeNode> queue = new LinkedList<>();\n        queue.add(root);\n        int maxLevel = 1;\n        int maxSum = Integer.MIN_VALUE;\n        int level = 1;\n\n        while (!queue.isEmpty()) {\n            int levelSum = 0;\n            int levelSize = queue.size();\n\n            for (int i = 0; i < levelSize; i++) {\n                TreeNode node = queue.poll();\n                levelSum += node.val;\n\n                if (node.left != null) {\n                    queue.add(node.left);\n                }\n                if (node.right != null) {\n                    queue.add(node.right);\n                }\n            }\n\n            if (levelSum > maxSum) {\n                maxSum = levelSum;\n                maxLevel = level;\n            }\n\n            level++;\n        }\n\n        return maxLevel;\n    }\n}\n\n```\n```C++ []\nclass Solution {\npublic:\n    int maxLevelSum(TreeNode* root) {\n        if (root == nullptr) {\n            return 0;\n        }\n\n        std::queue<TreeNode*> queue;\n        queue.push(root);\n        int maxLevel = 1;\n        int maxSum = INT_MIN;\n        int level = 1;\n\n        while (!queue.empty()) {\n            int levelSum = 0;\n            int levelSize = queue.size();\n\n            for (int i = 0; i < levelSize; i++) {\n                TreeNode* node = queue.front();\n                queue.pop();\n                levelSum += node->val;\n\n                if (node->left != nullptr) {\n                    queue.push(node->left);\n                }\n                if (node->right != nullptr) {\n                    queue.push(node->right);\n                }\n            }\n\n            if (levelSum > maxSum) {\n                maxSum = levelSum;\n                maxLevel = level;\n            }\n\n            level++;\n        }\n\n        return maxLevel;\n    }\n};\n\n```\n# An Upvote will be encouraging \uD83D\uDC4D
['Breadth-First Search', 'C++', 'Java', 'Python3']