[C++][Valid BST from Preorder] using stack sortable property


#1

using Stack sortable property, just verify if the input traversal vector does not contain the permutation pattern 231: three elements x, y, and z, appearing in the input in that respective order, with z < x < y.

int Solution::solve(vector<int> &A) {
    if(A.size() < 1)    return 1;
    stack<int> st;
    int rootNodeVal = INT_MIN;
    for(auto it: A) {
        if(it < rootNodeVal)   return 0;
        while(!st.empty() && st.top() < it) {
            rootNodeVal = st.top();
            st.pop();
        }
        st.push(it);
    }
    return 1;
}