My simple way 14 Line C++ solutions


  • 1
    L
    vector<int> postorderTraversal(TreeNode* root) {
        stack<TreeNode*> st; 
        vector<int> res;
        if(root!=nullptr) st.push(root);
        while(!st.empty()) {
            TreeNode *cur = st.top();
            st.pop();
            res.push_back(cur->val);
            if(cur->left) st.push(cur->left);
            if(cur->right) st.push(cur->right);
        }
        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.