Simple C++ recursion


  • 0
    S

    class Solution {
    public:
    vector<int> postorderTraversal(TreeNode* root) {

        vector<int> res;
        if(root==NULL) return res;
        
        if(root->left!=NULL){
            auto temp = postorderTraversal(root->left);
            res.insert(res.end(), temp.begin(), temp.end());
        }
        if(root->right!=NULL){
            auto temp = postorderTraversal(root->right);
            res.insert(res.end(), temp.begin(), temp.end());
        }
        res.push_back(root->val);
        
        return res;
    }
    

    };


Log in to reply
 

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