Solution By Dare2Solve | Detailed Explanation | Clean Code
Explanation []\nauthorslog.com/blog/MbCFvmdswp\n\n# Code\n\ncpp []\nclass Solution {\npublic:\n    int maxLevelSum(TreeNode* root) {\n        vector<int> sums;\
```Explanation []\nauthorslog.com/blog/MbCFvmdswp\n```\n# Code\n\n```cpp []\nclass Solution {\npublic:\n    int maxLevelSum(TreeNode* root) {\n        vector<int> sums;\n        traverse(root, 0, sums);\n        return max_element(sums.begin(), sums.end()) - sums.begin() + 1;\n    }\n    \nprivate:\n    void traverse(TreeNode* node, int depth, vector<int>& sums) {\n        if (!node) return;\n        \n        if (sums.size() == depth) {\n            sums.push_back(0);\n        }\n        \n        sums[depth] += node->val;\n        \n        traverse(node->left, depth + 1, sums);\n        traverse(node->right, depth + 1, sums);\n    }\n};\n```\n\n```python []\nclass Solution:\n    def maxLevelSum(self, root: Optional[TreeNode]) -> int:\n        sums = []\n        \n        def traverse(node, depth):\n            if not node:\n                return\n            \n            if len(sums) == depth:\n                sums.append(0)\n            \n            sums[depth] += node.val\n            \n            traverse(node.left, depth + 1)\n            traverse(node.right, depth + 1)\n        \n        traverse(root, 0)\n        return sums.index(max(sums)) + 1\n```\n\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 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        List<Integer> sums = new ArrayList<>();\n        traverse(root, 0, sums);\n        return sums.indexOf(Collections.max(sums)) + 1;\n    }\n\n    private void traverse(TreeNode node, int depth, List<Integer> sums) {\n        if (node == null) return;\n        \n        if (depth >= sums.size()) {\n            sums.add(0);\n        }\n        \n        sums.set(depth, sums.get(depth) + node.val);\n        \n        traverse(node.left, depth + 1, sums);\n        traverse(node.right, depth + 1, sums);\n    }\n}\n```\n\n```javascript []\n/**\n * Definition for a binary tree node.\n * function TreeNode(val, left, right) {\n *     this.val = (val===undefined ? 0 : val)\n *     this.left = (left===undefined ? null : left)\n *     this.right = (right===undefined ? null : right)\n * }\n */\nvar maxLevelSum = function(root) {\n    const sums = [];\n\n    const traverse = (node, depth) => {\n        if (!node) {\n            return;\n        }\n\n        sums[depth] = (sums[depth] ?? 0) + node.val;\n\n        traverse(node.left, depth + 1);\n        traverse(node.right, depth + 1);\n    }\n    traverse(root, 0);\n    return sums.findIndex((el) =>  el === Math.max(...sums)) + 1;\n};\n```
['Depth-First Search', 'Breadth-First Search', 'Python', 'C++', 'Java', 'Python3', 'JavaScript']