Easy Solution + Video| Java C++ Python
Intuition:\nAs the name indicates level order traversal is traversing the tree level by level.This can easily achieved with the use of a queue.\nBut there is an
# Intuition:\nAs the name indicates level order traversal is traversing the tree level by level.This can easily achieved with the use of a queue.\nBut there is an additional requirment that the sum of each level and the level itself has to be remembered. This can be achieved with slight modification to L.O.T i.e we get the size of the queue at the beginning of each loop and traverse all the elements in a for loop.\n\n#Approach\n1. Initialize a queue and add the root.\n2. Initialize maxSum, ans and level.\n3. while queue is not empty:\n\t4. Get the size of queue and store in levelSize.\n\t5. for i = 0 to level size remove all the elements from queue and add it to levelSum.\n\t6. if levelSum> maxSum change both maxSum and ans.\n\t7. level++\n\n8. Return ans.\n\n<iframe width="560" height="315" src="https://www.youtube.com/embed/nHFygbXqhcM" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen></iframe>\n\n```\nclass Solution {\n    public int maxLevelSum(TreeNode root) {\n        Queue<TreeNode> queue = new LinkedList();\n        queue.add(root);\n        int maxSum = root.val;\n        int ans = 1;\n        int level = 1;\n        while (!queue.isEmpty()){\n            int levelSize = queue.size();\n            int levelSum = 0;\n            for(int i = 0;i<levelSize;i++){\n                TreeNode removed = queue.remove();\n                levelSum+=removed.val;\n                if(removed.left!=null)queue.add(removed.left);\n                if(removed.right!=null) queue.add(removed.right);\n            }\n            if(levelSum>maxSum){\n                maxSum = levelSum;\n                ans = level;\n            }\n            level++;\n        }\n        return ans;\n    }\n}\n```\n\n```\nclass Solution {\npublic:\n    int maxLevelSum(TreeNode* root) {\n        std::queue<TreeNode*> queue;\n        queue.push(root);\n        int maxSum = root->val;\n        int ans = 1;\n        int level = 1;\n        while (!queue.empty()) {\n            int levelSize = queue.size();\n            int levelSum = 0;\n            for (int i = 0; i < levelSize; i++) {\n                TreeNode* removed = queue.front();\n                queue.pop();\n                levelSum += removed->val;\n                if (removed->left != nullptr) queue.push(removed->left);\n                if (removed->right != nullptr) queue.push(removed->right);\n            }\n            if (levelSum > maxSum) {\n                maxSum = levelSum;\n                ans = level;\n            }\n            level++;\n        }\n        return ans;\n    }\n};\n```\n\n```\nclass Solution:\n    def maxLevelSum(self, root: TreeNode) -> int:\n        queue = deque()\n        queue.append(root)\n        maxSum = root.val\n        ans = 1\n        level = 1\n        while queue:\n            levelSize = len(queue)\n            levelSum = 0\n            for _ in range(levelSize):\n                removed = queue.popleft()\n                levelSum += removed.val\n                if removed.left:\n                    queue.append(removed.left)\n                if removed.right:\n                    queue.append(removed.right)\n            if levelSum > maxSum:\n                maxSum = levelSum\n                ans = level\n            level += 1\n        return ans\n```\n
['C', 'Python', 'Java']