13 lines concise and easy understand c++ solution iterative version


  • 3
    A
    class Solution {
    public:
        vector<int> postorderTraversal(TreeNode* root) {
            stack<TreeNode*> st;
            vector<int> res;
            while(!st.empty() || root){
                if(root){
                    st.push(root);
                    res.push_back(root->val);
                    root = root->right;
                    continue;
                }
                root = st.top()->left;
                st.pop();
            }
            reverse(res.begin(), res.end());
            return res;
        }
    };

Log in to reply
 

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