Simple py,JAVA code explained!!(BFS)
Approach:BFS\n# Intuition\n Describe your first thoughts on how to solve this problem. \n- we calculate sum of each level and store it in maximum.\n- then we fi
# Approach:BFS\n# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n- we calculate `sum` of each level and store it in `maximum`.\n- then we find the `max` of maximum.\n- Then return the `index+1`,because of 1-based index for each level,where `index` is the first `max` of maximum.\n\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:$O(n)$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:$O(n)$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```Python3 []\n# Definition for a binary tree node.\n# class TreeNode:\n#     def __init__(self, val=0, left=None, right=None):\n#         self.val = val\n#         self.left = left\n#         self.right = right\nclass Solution:\n    def maxLevelSum(self, root: Optional[TreeNode]) -> int:\n        if root.left is None and root.right is None:\n            return 1\n        def BFS(root):\n            q=deque([root])\n            maximum=[]\n            maximum.append(root.val)\n            while q:\n                l=len(q)\n                value=0\n                for i in range(l):\n                    tempRoot=q.popleft()\n                    if tempRoot.left:\n                        q.append(tempRoot.left)\n                        value+=tempRoot.left.val\n                    if tempRoot.right:\n                        q.append(tempRoot.right)\n                        value+=tempRoot.right.val\n                print(maximum)\n                maximum.append(value)\n            maximum.pop()\n            print(maximum)\n            #print(list(q))\n            return maximum.index(max(maximum))+1\n        return BFS(root)\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 Solution {\n    private int BFS(TreeNode root){\n        int i,l;\n        Queue<TreeNode> q=new LinkedList<>();\n        List<Integer> maximum=new ArrayList<>();\n        maximum.add(root.val);\n        q.add(root);\n        while(!q.isEmpty()){\n            l=q.size();\n            int value=0;\n            for(i=0;i<l;i++){\n                TreeNode r=q.poll();\n                if(r.left!=null){\n                    value+=r.left.val;\n                    q.add(r.left);\n                }\n                if(r.right!=null){\n                    value+=r.right.val;\n                    q.add(r.right);\n                }\n            }\n            maximum.add(value);\n        }\n        maximum.remove(maximum.size()-1);\n        int maxNum = Collections.max(maximum);\n        return maximum.indexOf(maxNum) + 1;\n    }\n    public int maxLevelSum(TreeNode root) {\n        if(root.left==null && root.right==null){\n            return 1;\n        }\n        return BFS(root);\n    }\n}\n```
['Breadth-First Search', 'Binary Tree', 'Java', 'Python3']