Standard way C++


  • 0
    H

    class Solution {
    public:
    vector<int> postorderTraversal(TreeNode* root) {
    stack<TreeNode*> nodeStack;
    TreeNode* lastNodeVisited;
    vector<int> nodeValues;
    if(!root) return nodeValues;
    while(! nodeStack.empty() || root != NULL)
    {
    if(root != NULL)
    {
    nodeStack.push(root);
    root = root->left;
    }
    else
    {
    TreeNode* peakNode = nodeStack.top();
    if(peakNode->right != NULL && lastNodeVisited != peakNode->right)
    {
    root = peakNode->right;
    }
    else
    {
    nodeValues.push_back(peakNode->val);
    lastNodeVisited = peakNode;
    nodeStack.pop();
    }
    }
    }
    return nodeValues;
    }
    };


Log in to reply
 

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