Java Brute Force Solution
Intuition\n Describe your first thoughts on how to solve this problem. \nTo solve this problem, the first thought that comes to mind is to traverse the binary t
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nTo solve this problem, the first thought that comes to mind is to traverse the binary tree level by level, calculate the sum of the nodes\' values for each level, and then determine which level has the maximum sum.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n- Use a queue to perform a level order traversal (BFS) of the binary tree.\n- For each level, calculate the sum of the node values.\n- Track the level with the maximum sum encountered so far.\n- Return the level number with the maximum sum.\n\n# Complexity\n- Time complexity: O(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(n)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\n/**\n * Definition for a binary tree node.\n * public class TreeNode {\n *     int val;\n *     TreeNode left;\n *     TreeNode right;\n *     TreeNode() {}\n *     TreeNode(int val) { this.val = val; }\n *     TreeNode(int val, TreeNode left, TreeNode right) {\n *         this.val = val;\n *         this.left = left;\n *         this.right = right;\n *     }\n * }\n */\nclass Solution {\n    public int maxLevelSum(TreeNode root) {\n        if (root == null){\n            return 0;\n        }\n        Queue<TreeNode> q = new LinkedList<>();\n        q.add(root);\n        q.add(null);\n        ArrayList<Integer> arr = new ArrayList<>();\n        int sum = 0;\n        while (!q.isEmpty()) {\n            TreeNode curr = q.remove();\n            if (curr == null) {\n                arr.add(sum);\n                sum = 0;\n                if (q.isEmpty()) {\n                    break;\n                }\n                else {\n                    q.add(null);\n                }\n            }\n            else {\n                sum += curr.val;\n\n                if (curr.left != null) {\n                    q.add(curr.left);\n                }\n                if (curr.right != null) {\n                    q.add(curr.right);\n                }\n            }\n        }\n        int idx = 0;\n        int s = Integer.MIN_VALUE;\n        for (int i = 0; i < arr.size(); i++) {\n            if (arr.get(i) > s) {\n                idx = i + 1;\n                s = arr.get(i);\n            }\n        }\n        return idx;\n    }\n}\n```
['Java']