Easy Simple - Fast C || C++ || Java solution beats 100%
Intuition\n\n\n\nJava []\n/**\n * Definition for a binary tree node.\n * public class TreeNode {\n * int val;\n * TreeNode left;\n * TreeNode right;\n * TreeNod
# Intuition\n![Screenshot 2024-02-04 140355.png](https://assets.leetcode.com/users/images/4e365f1e-3071-465a-81fc-ca3bdbaaf48c_1707035735.9300387.png)\n![Screenshot 2024-02-04 140345.png](https://assets.leetcode.com/users/images/33664032-ca0f-4078-89b0-dd6a36f08f56_1707035740.4802642.png)\n![Screenshot 2024-02-04 140334.png](https://assets.leetcode.com/users/images/2e7a72d0-24d2-4468-99e0-56a2a2cf52c2_1707035746.212304.png)\n```Java []\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        Queue<TreeNode> que = new LinkedList<>();\n        int level = 0, max = Integer.MIN_VALUE, cnt = 0;\n        que.add(root);\n        while (!que.isEmpty()) {\n            cnt++;\n            int sum = 0;\n            int n = que.size();\n            for (; n > 0; n--) {\n                TreeNode cur = que.remove();\n                sum += cur.val;\n                if (cur.left != null)\n                    que.add(cur.left);\n                if (cur.right != null)\n                    que.add(cur.right);\n            }\n\n            if (sum > max) {\n                max = sum;\n                level = cnt;\n            }\n        }\n        return level;\n    }\n}\n```\n```C++ []\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n *     int val;\n *     TreeNode *left;\n *     TreeNode *right;\n *     TreeNode() : val(0), left(nullptr), right(nullptr) {}\n *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left),\n * right(right) {}\n * };\n */\nclass Solution {\npublic:\n    int maxLevelSum(TreeNode* root) {\n        int ans;\n        int maxsum = INT_MIN;\n        if (root == NULL)\n            return ans;\n        queue<TreeNode*> q;\n        q.push(root);\n        int level = 1;\n        while (!q.empty()) {\n            int sum = 0;\n            int size = q.size();\n            for (int i = 0; i < size; i++) {\n                TreeNode* curr = q.front();\n                q.pop();\n                sum += curr->val;\n                if (curr->left != NULL)\n                    q.push(curr->left);\n                if (curr->right != NULL)\n                    q.push(curr->right);\n            }\n            if (sum > maxsum) {\n                ans = level;\n                maxsum = max(sum, maxsum);\n            }\n            level++;\n        }\n        return ans;\n    }\n};\n```\n```C []\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n *     int val;\n *     struct TreeNode *left;\n *     struct TreeNode *right;\n * };\n */\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n *     int val;\n *     struct TreeNode *left;\n *     struct TreeNode *right;\n * };\n */\ntypedef struct TreeNode node;\nint get_height(node* root) {\n    if (!root)\n        return 0;\n    int l = get_height(root->left) + 1;\n    int r = get_height(root->right) + 1;\n    return l > r ? l : r;\n}\nvoid get_sum(node* root, int* data, int level) {\n    if (root) {\n        data[level] += root->val;\n        get_sum(root->left, data, level + 1);\n        get_sum(root->right, data, level + 1);\n    }\n}\nint get_max_idx(int* arr, int n) {\n    int max = 0;\n    for (int i = 0; i < n; i++)\n        max = arr[i] > arr[max] ? i : max;\n    return max + 1;\n}\nint maxLevelSum(struct TreeNode* root) {\n    int n = get_height(root);\n    int* data = (int*)calloc(n, sizeof(int));\n    get_sum(root, data, 0);\n    return get_max_idx(data, n);\n}\n```\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n     O(n)\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n     O(n)\n# Code\n```\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n *     int val;\n *     TreeNode *left;\n *     TreeNode *right;\n *     TreeNode() : val(0), left(nullptr), right(nullptr) {}\n *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left),\n * right(right) {}\n * };\n */\nclass Solution {\npublic:\n    int maxLevelSum(TreeNode* root) {\n        int ans;\n        int maxsum = INT_MIN;\n        if (root == NULL)\n            return ans;\n        queue<TreeNode*> q;\n        q.push(root);\n        int level = 1;\n        while (!q.empty()) {\n            int sum = 0;\n            int size = q.size();\n            for (int i = 0; i < size; i++) {\n                TreeNode* curr = q.front();\n                q.pop();\n                sum += curr->val;\n                if (curr->left != NULL)\n                    q.push(curr->left);\n                if (curr->right != NULL)\n                    q.push(curr->right);\n            }\n            if (sum > maxsum) {\n                ans = level;\n                maxsum = max(sum, maxsum);\n            }\n            level++;\n        }\n        return ans;\n    }\n};\n```\n![th.jpeg](https://assets.leetcode.com/users/images/483c044e-e318-40b6-9f04-084fdb82c561_1707035946.5471888.jpeg)\n
['Tree', 'Depth-First Search', 'Breadth-First Search', 'C', 'C++', 'Java']