void aveLevHelp(vector<double> &res,vector<int> &cnt,unsigned int lev,TreeNode *r) { if(r==NULL) return; if(lev+1>res.size()) { res.push_back(0); cnt.push_back(0); } res[lev]+=r->val; cnt[lev]++; aveLevHelp(res,cnt,lev+1,r->left); aveLevHelp(res,cnt,lev+1,r->right); } vector<double> averageOfLevels(TreeNode* r) { vector<double> res; vector<int> cnt; aveLevHelp(res,cnt,0,r); for(unsigned int i=0;i<res.size();++i) { res[i]/=cnt[i]; } return res; }
转载于:https://www.cnblogs.com/xLester/p/7570283.html