Share my C++ Solution


  • 2
    V
        vector<int> postorderTraversal(TreeNode* root) {
            vector<int> result;
            if(root == NULL)
                return result;
            stack<TreeNode* > st;
            int expVal = INT_MIN; 
            st.push(root);
            while(!st.empty()) {
                root = st.top();
                if((root->left && root->left->val == expVal) ||
                    (root->right && root->right->val == expVal) ||
                     (root->left == NULL && root->right == NULL)) {
                    result.push_back(expVal = root->val);
                    st.pop();
                    continue;
                }
                if(root->right)
                    st.push(root->right);
                if(root->left)
                    st.push(root-> left);
            }
            return result;
        }

Log in to reply
 

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