Accepted C++ code using recursion


  • -14
    T
    class Solution {
    public:
        vector<int> result;
        vector<int> postorderTraversal(TreeNode *root)
        {
            postorderTraversa(root);
            reverse(result.begin(),result.end());
            return result;
        }
        void postorderTraversa(TreeNode *root)
        {
            if(root!=NULL) 
            {
            result.push_back(root->val);
            postorderTraversa(root->right);
            postorderTraversa(root->left);
            
            }
        }
    };

  • 0
    R

    They said recursion solution is trivial


Log in to reply
 

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