19 line C++ 8ms Solution, very easy


  • 9
    O
    class Solution {
    protected:
        vector<vector<int>> ans;
        void dfs(TreeNode *root, int height){
            if (root == NULL) 
                return;
            while (ans.size() <= height)
                ans.push_back(vector<int>());
            ans[height].push_back(root->val);
            dfs(root->left, height + 1);
            dfs(root->right, height + 1);
        }
    
    public:
        vector<vector<int>> levelOrderBottom(TreeNode* root) {
           dfs(root, 0);
           reverse(ans.begin(), ans.end());
            return ans;    
        }
    };

  • 0
    X

    vector<int>() what's this?temporary vector?


  • 0
    O

    vector() - call default constructor, create new vector whose size = 0


Log in to reply
 

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