Simple C++ Solution with Recursion


  • 0
    M
    class Solution {
    public:
        void Fill(TreeNode * root, int depth, vector<vector<int>> & result) {
            if (!root) return;
            if (result.size() < depth + 1) result.resize(depth + 1);
            result[depth].push_back(root->val);
            Fill(root->left, depth + 1, result);
            Fill(root->right, depth + 1, result);
        }
        vector<vector<int>> levelOrderBottom(TreeNode* root) {
            vector<vector<int>> result;
            Fill(root, 0, result);
            reverse(result.begin(), result.end());
            return result;
        }
    };
    

Log in to reply
 

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