Best O(N) Solution
Approach\nUsing BFS\n\n# Complexity\n- Time complexity:\nO(n)\n\n- Space complexity:\nO(n)\n\n# Code\n\n/**\n * Definition for a binary tree node.\n * struct Tr
# Approach\nUsing BFS\n\n# Complexity\n- Time complexity:\n$$O(n)$$\n\n- Space complexity:\n$$O(n)$$\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), right(right) {}\n * };\n */\nclass Solution {\npublic:\n    int maxLevelSum(TreeNode* root) {\n        int ans;\n        int maxSum = INT_MIN, level = 0;\n        queue<TreeNode*> q;\n        q.push(root);\n        while (!q.empty()) {\n            int nodeCount = q.size();\n            int curSum = 0;\n            while (nodeCount > 0) {\n                TreeNode* node = q.front();\n                q.pop();\n                curSum += node->val;\n                if (node->left != NULL)\n                    q.push(node->left);\n                if (node->right != NULL)\n                    q.push(node->right);\n                nodeCount--;\n            }\n            level++;\n            if (curSum > maxSum) {\n                maxSum = curSum;\n                ans = level;\n            }\n        } \n        return ans;   \n    }\n};\n```
['C++']