[Java/Python 3] Two codes / language: BFS level traversal and DFS level sum.
Method 1: BFS\n\nUse BFS to find the sum of each level, then locate the level with largest sum.\n\n\njava\n    public int maxLevelSum(TreeNode root) {\n        
**Method 1: BFS**\n\nUse BFS to find the sum of each level, then locate the level with largest sum.\n\n\n```java\n    public int maxLevelSum(TreeNode root) {\n        int max = Integer.MIN_VALUE, maxLevel = 1;\n        Queue<TreeNode> q = new LinkedList<>();\n        q.offer(root);\n        for (int level = 1; !q.isEmpty(); ++level) {\n            int sum = 0;\n            for (int sz = q.size(); sz > 0; --sz) {\n                TreeNode n = q.poll();\n                sum += n.val;\n                if (n.left != null) { \n                    q.offer(n.left);\n                }\n                if (n.right != null) {\n                    q.offer(n.right);\n                }\n            }\n            if (max < sum) {\n                max = sum;\n                maxLevel = level;\n            }\n        }\n        return maxLevel;\n    }\n```\n```python\n    def maxLevelSum(self, root: TreeNode) -> int:\n        max, level, maxLevel = -float(\'inf\'), 0, 0\n        q = collections.deque()\n        q.append(root)\n        while q:\n            level += 1\n            sum = 0\n            for _ in range(len(q)):\n                node = q.popleft()\n                sum += node.val\n                if node.left:\n                    q.append(node.left)\n                if node.right:\n                    q.append(node.right)\n            if max < sum:\n                max, maxLevel = sum, level        \n        return maxLevel\n```\n\nPython 3 BFS code - credit to **@leihao1313**\n```python\n    def maxLevelSum(self, root: TreeNode) -> int:\n        ans, q, depth = (-math.inf, 0), [root], -1\n        while q:\n            ans = max(ans, (sum(node.val for node in q), depth))\n            q = [kid for node in q for kid in (node.left, node.right) if kid]\n            depth -= 1\n        return -ans[1]\n```\t\t\n\n---------------------\n\n**Method 2: DFS**\nUse DFS to compute and store the sum of each level in an ArrayList, then locate the level with largest sum.\n1. Recurse down from root, level of which is 0, increase level by 1 for each recursion down;\n2. Use the level as the index of an ArrayList to store the sum of the correspoinding level;\n3. Find the index of the max sum, then plus 1.\n\n**Java**\n\n```\n    public int maxLevelSum(TreeNode root) {\n        List<Integer> list = new ArrayList<>();\n        dfs(root, list, 0);\n        return 1 + IntStream.range(0, list.size()).reduce(0, (a, b) -> list.get(a) < list.get(b) ? b : a);\n    }\n    private void dfs(TreeNode n, List<Integer> l, int level) {\n        if (n == null) { return; } \n        if (l.size() == level) { l.add(n.val); } // never reach this level before, add first value.\n        else { l.set(level, l.get(level) + n.val); } // reached the level before, accumulate current value to old value.\n        dfs(n.left, l, level + 1);\n        dfs(n.right, l, level + 1);\n    }\n```\nIn case you are NOT comfortable with the Java 8 stream in the return statement, it can be written as:\n```\n        int maxLevel = 0;\n        for (int i = 0; i < list.size(); ++i) {\n            if (list.get(maxLevel) < list.get(i)) {\n                maxLevel = i;\n            }\n        }\n        return maxLevel + 1;\n```\n\n----\n\n**Python 3**\n\n```\n    def maxLevelSum(self, root: TreeNode) -> int:\n        def dfs(node: TreeNode, list: List, level: int) -> None:\n            if not node:\n                return\n            if len(list) == level:\n                list.append(node.val)\n            else:\n                list[level] += node.val\n            dfs(node.left, list, level + 1)\n            dfs(node.right, list, level + 1)\n        list = []    \n        dfs(root, list, 0)\n        return 1 + list.index(max(list))\n```\n\n----\n\n**Analysis:**\n\nTime & space: `O(n), n` is the number of total nodes.
['Depth-First Search', 'Breadth-First Search', 'Java', 'Python3']