C++ 13ms Clean Solution


  • 0
    G

    class Solution {
    public:
    vector<double> res;
    vector<int> div;

    void get(TreeNode* root, int level)
    {
        if (root == NULL) return;
        if (res.size() == level) 
        {
            res.push_back(root->val);
            div.push_back(1);
        }
        else
        {
            res[level] += root->val;
            div[level]++;
        }
        level++;
        get(root->left, level);
        get(root->right, level);
    }
    
    vector<double> averageOfLevels(TreeNode* root) {
        get(root, 0);
        for (int i = 0; i < res.size(); i++) res[i] /= div[i];
        return res;
    }
    

    };


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.