JAVA 98% | 100% - 4 line recursion - Explanation + Optimization
Normally we would use a HashMap with level/height being the key and node.val being the value to solve this problem. This will complete the problem with a 7% spe
Normally we would use a HashMap with level/height being the key and node.val being the value to solve this problem. This will complete the problem with a 7% speed.\n\nHowever, since the number of levels is at most 10^4, we can use a int[] map. The level/height will be the index position and the sum will be the index element. This will dramatically speed up the algo because we are avoiding the hashing and size increase.\n\n```\nclass Solution {\n    public int maxLevelSum(TreeNode root) {\n        int[] map = new int[(int)Math.pow(10, 4)+1];\n        recur(root, map, 1);\n        int maxLevel = 1;\n        for(int i = 0; i < map.length; i++) if(map[i] > map[maxLevel]) maxLevel = i;\n        return maxLevel;\n    }\n    \n    public void recur(TreeNode root, int[] map, int level) {\n        if(root == null) return;\n        map[level] += root.val;\n        recur(root.right, map, level + 1);\n        recur(root.left, map, level + 1);\n    }\n}\n```
[]