Recursive solution in c++


  • 0
    Z

    here's my short and easy to understand recursive solution in c++

       class Solution {
            vector<int> result;
        public:
            vector<int> postorderTraversal(TreeNode *root) {
                result.clear();
                POT_helper(root);
                return result;
            }
        
        void POT_helper(TreeNode *n) {
            if (n==NULL) return;
    
            if (n->left != NULL) {
                POT_helper(n->left);
            }
            if (n->right != NULL) {
                POT_helper(n->right);
            }
            result.push_back(n->val);
        }
    };

  • 0
    U

    Here is my solution , however , it's correct in my gcc , but error in LeetCode , Why?

    vector<int> postorderTraversal(TreeNode *root) {

        static std::vector<int> v;
    
        if (root == NULL)
    
            return v;
    
        postorderTraversal(root->left);
    
        postorderTraversal(root->right);
    
        v.push_back(root->val);
    
        return v;        
    
    }

Log in to reply
 

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