share my solution with C++ 0ms


  • 0
    G
    class Solution {
    public:
        vector<int> postorderTraversal(TreeNode* root) {
            vector<int> result;
            vector<TreeNode*> mystack;
            TreeNode * curr=root;
            TreeNode *pre=NULL;
            while(!mystack.empty()||curr)
            {
                while(curr)
                {
                    mystack.push_back(curr);
                    curr=curr->left;
                }
                TreeNode *last=mystack.back();
                if(last->right==NULL||last->right==pre)
                {
                    pre=last;
                    result.push_back(last->val);
                    mystack.pop_back();
                }else
                {
                    curr=last->right;
                }
            }
            return result;
        }
    };
    

Log in to reply
 

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