剑指OFFER----面试题33. 二叉搜索树的后序遍历序列

链接:https://leetcode-cn.com/problems/er-cha-sou-suo-shu-de-hou-xu-bian-li-xu-lie-lcof/

代码:

class Solution {
public:

    bool verifyPostorder(vector<int>& postorder) {
        return dfs(postorder, 0, postorder.size() - 1);
    }

    bool dfs(vector<int>& postorder, int l, int r) {
        if (l >= r) return true;
        int root = postorder[r];
        int k = l;
        while (k < r && postorder[k] < root) k++;
        int s = k;
        while (s < r && postorder[s] > root) s++;
        if (s != r) return false;
        return dfs(postorder, l, k - 1) && dfs(postorder, k + 1, r - 1);
    }
};
原文地址:https://www.cnblogs.com/clown9804/p/12380603.html