Level Order. Readable code - Python / C++
\n# Code\n\npython []\nclass Solution:\n    def maxLevelSum(self, root: Optional[TreeNode]) -> int:\n        a = []\n\n        def dfs(node, h):\n            if
\n# Code\n\n```python []\nclass Solution:\n    def maxLevelSum(self, root: Optional[TreeNode]) -> int:\n        a = []\n\n        def dfs(node, h):\n            if not node:\n                return\n            if len(a) == h:\n                a.append([])\n\n            dfs(node.left, h+1)\n            a[h].append(node.val)\n            dfs(node.right, h+1)\n\n        dfs(root, 0)\n\n        level_sum = [sum(level) for level in a]\n        maximal = max(level_sum)\n        \n        return 1 + level_sum.index(maximal)\n```\n```C++ []\nclass Solution {\nprivate:\n    void dfs(TreeNode* node, int h, vector<vector<int> > &a) {\n        if(!node) return;\n\n        if(a.size() == h) {\n            a.push_back({});\n        }\n\n        dfs(node->left, h+1, a);\n        a[h].push_back(node->val);\n        dfs(node->right, h+1, a);\n    }\npublic:\n    int maxLevelSum(TreeNode* root) {\n        vector<vector<int>>a;\n        int sm = 0;\n        dfs(root, 0, a);\n\n        vector<int> levelSum;\n\n        for(auto &level:a) {\n            for(auto ele:level) {\n                sm += ele;\n            }\n            levelSum.push_back(sm);\n            sm = 0;\n        }\n\n        int maxSum = *max_element(levelSum.begin(), levelSum.end());\n        \n        for(int i = 0; i < levelSum.size(); ++i) {\n            if(levelSum[i] == maxSum) {\n                return i+1;\n            }\n        }\n        return -1;\n    }\n};\n```\n
['Python3']