✅ Explained - Simple and Clear Python3 Code✅
\n# Approach\nThe logic of the code involves recursively traversing the binary tree and maintaining a list of sums at each level. Starting from the root, the co
\n# Approach\nThe logic of the code involves recursively traversing the binary tree and maintaining a list of sums at each level. Starting from the root, the code calculates the sum of node values at each level and stores them in the list. After the traversal, it determines the maximum sum from the list and returns the corresponding level. The logic ensures that each node\'s value is added to the sum of its level and continues recursively for the left and right children, incrementing the level. Ultimately, it finds the level with the maximum sum of node values in the binary tree.\n\n\n# Complexity\n- Time complexity:\nO(n), as it has to process each node in the tree.\n- Space complexity:\nThe memory complexity of the code is O(n), as the list \'sm\' is used to store the sums at each level. The size of this list can grow up to the number of levels in the tree, which can be at most n.\n# Code\n```\nclass Solution:\n    def maxLevelSum(self, root: Optional[TreeNode]) -> int:\n        sm = [0]\n        def cal(r,lvl):\n            if len(sm)<lvl:\n                sm.append(0)\n            sm[lvl-1]+=r.val\n            if r.right:\n                cal(r.right,lvl+1)\n            if r.left:\n                cal(r.left,lvl+1)\n        cal(root,1)\n        mx=max(sm)\n        for idx, x in enumerate(sm):\n            if x == mx:\n                return idx+1\n\n```
['Python3']