Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Definition for a binary tree node.
- * struct TreeNode {
- * int val;
- * TreeNode *left;
- * TreeNode *right;
- * TreeNode() : val(0), left(nullptr), right(nullptr) {}
- * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
- * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
- * };
- */
- class Solution {
- public:
- queue<pair<int,int>> q;
- int mxl=INT_MIN;
- int maxLevelSum(TreeNode* root)
- {
- if(root==NULL)
- {
- return root->val;
- }
- recursion(root,0);
- vector<int> v(mxl+1,0);
- while(!q.empty())
- {
- int fir=q.front().first;
- int sec=q.front().second;
- v[sec]+=fir;
- q.pop();
- }
- int mx=INT_MIN;
- int ans=-1;
- for(int i=0;i<v.size();i++)
- {
- if(v[i]>mx)
- {
- mx=v[i];
- ans=i;
- }
- }
- return ans+1;
- }
- void recursion(TreeNode* root,int level)
- {
- if(root==NULL)
- {
- return;
- }
- mxl=max(mxl,level);
- q.push({root->val,level});
- recursion(root->left,level+1);
- recursion(root->right,level+1);
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement