✅ EASY || 💯 98 BEATS-PROOF || 🌟 JAVA || 🧑‍💻 BEGINNER FRIENDLY || 🙂 DETAILED EXPLANATION
\n\n# \uD83C\uDF1F Proof\n---\n\n\n---\n\n\n\n\n# \uD83C\uDF1F Intuition\n---\n Describe your first thoughts on how to solve this problem. \n### To identify the
```\n```\n# \uD83C\uDF1F Proof\n---\n![image.png](https://assets.leetcode.com/users/images/839e3818-b839-438e-8567-712c4bd3c144_1731905811.145253.png)\n\n---\n\n```\n```\n\n# \uD83C\uDF1F Intuition\n---\n<!-- Describe your first thoughts on how to solve this problem. -->\n### **To identify the level in a binary tree with the highest sum of node values**:\n\n1. **Traverse the tree while maintaining the current level of nodes.**\n2. **Compute the sum of nodes for each level during traversal.**\n3. **Compare the sums at all levels to determine the maximum.**\n\n```\n```\n# \uD83C\uDF1F Approach\n---\n<!-- Describe your approach to solving the problem. -->\n1. ### **Initialization**:\n\n    - Use an array `sumatlevels[]` to store the sum of node values for each level. The index of the array corresponds to the level of the tree.\n    - Use a variable `totallevel` to keep track of the maximum depth encountered during the traversal.\n\n2. ### **Recursive Function**:\n\n    - The recursive helper function `maxlevelsumhelper()` traverses the binary tree.\n    - For each node:\n        - Add its value to the corresponding level in the `sumatlevels` array.\n        - Update the `totallevel` to ensure all levels are tracked.\n    - First visit the right subtree, then the left subtree to ensure proper level traversal.\n\n3. ### **Post-Traversal**:\n\n    - Iterate through the `sumatlevels` array up to the maximum level (`totallevel`) to find the level with the highest sum.\n    - Return the 1-based index of this level.\n\n4. ### **Optimization**:\n\n    - The use of an array ensures O(1) access for updating and retrieving level sums.\n    - Using `System.gc()` helps manage memory for large input cases, though it\u2019s optional.\n```\n```\n# \uD83C\uDF1F Complexity\n---\n- ### Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n**$$O(n)$$, where $$n$$ is the number of nodes in the tree, as each node is visited once.**\n- ### Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n**$$O(h)$$, where $$h$$ is the height of the tree, for recursive stack space.**\n```\n```\n# \uD83C\uDF1F Code\n---\n```java []\n\nclass Solution {\n    private int totallevel;\n    private int sumatlevels[] = new int[10000];\n\n    public int maxLevelSum(TreeNode root) {\n        int ans =1,maxsum = Integer.MIN_VALUE;\n\n        maxlevelsumhelper(root,0);\n\n        for(int i=0;i<=totallevel;i++){\n            int sum = sumatlevels[i];\n\n            if(sum>maxsum){\n                maxsum = sum;\n                ans = i+1;\n            }\n        }\n\n        System.gc();\n\n        return ans;\n    }\n\n    private void maxlevelsumhelper(TreeNode t,int l){\n        if(t == null)\n            return;\n\n        sumatlevels[l]+=t.val;\n        \n        totallevel = Math.max(totallevel,l);\n\n        maxlevelsumhelper(t.right,l+1);\n        maxlevelsumhelper(t.left,l+1);\n    }\n}\n```\n\n```\n```\n---\n![upvote.png](https://assets.leetcode.com/users/images/3c04d8ab-128f-400e-adbf-a1af4cd19c34_1731907804.1652973.png)\n\n---\n```\n```\n\n
['Array', 'Breadth-First Search', 'Recursion', 'Binary Tree', 'Java']