LeetCode "Verify Preorder Sequence in Binary Search Tree"

A typical recursion one.

class Solution 
{
    bool _verify(vector<int> &in, int i, int j, 
                int low, int high)
    {
        if (i > j) return true; // empty
        
        int root = in[i];
        if (!(root >= low && root <= high)) return false;
        if (i == j) return true;

        //    find split
        int inx = i + 1;
        while (inx <= j && in[inx] < root)
            inx++;

        return _verify(in, i + 1, inx - 1, low, root - 1) &&
            _verify(in, inx, j, root + 1, high);
    }
public:
    bool verifyPreorder(vector<int>& preorder) 
    {
        return _verify(preorder, 0, preorder.size() - 1,
                        INT_MIN, INT_MAX);
    }
};
原文地址:https://www.cnblogs.com/tonix/p/4745195.html