【剑指offer】【树】33. 二叉搜索树的后序遍历序列
题目链接:https://leetcode-cn.com/problems/er-cha-sou-suo-shu-de-hou-xu-bian-li-xu-lie-lcof/
递归
class Solution {
public:
vector<int> seq;
bool verifyPostorder(vector<int>& postorder) {
seq = postorder;
return dfs(0, seq.size() - 1);
}
bool dfs(int l , int r)
{
if(l >= r) return true;
int root = seq[r];
int k = l;
while(k < r && seq[k] < root) k++;
for(int i = k; i < r; i++)
if(seq[i] < root) return false;
return dfs(l, k - 1) && dfs(k, r - 1);
}
};
知识的价值不在于占有,而在于使用