0ms with c++ iteratively


  • 0
    M

    vector<int> postorderTraversal(TreeNode* root) {
    vector<int> vt;
    stack<TreeNode*> St;
    TreeNode *last=NULL;
    TreeNode *current=root;
    while(!St.empty()||current)
    {
    if(current)
    {
    St.push(current);
    current=current->left;
    }
    else
    {
    TreeNode *temp=St.top();
    if(temp->right&&temp->right!=last)
    current=temp->right;
    else
    {
    vt.push_back(temp->val);
    St.pop();
    last=temp;
    }
    }
    }
    return vt;
    }


Log in to reply
 

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