二叉搜索树的后续遍历序列(边界条件怎样确定)
二叉搜索树的后序遍历序列
https://www.nowcoder.com/practice/a861533d45854474ac791d90e447bafd?tpId=13&tqId=11176&tPage=2&rp=2&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking
解法1:
class Solution { public: bool VerifySquenceOfBST(vector<int> sequence) { return bst(sequence,0,sequence.size()-1); } bool bst(vector<int>& sequence,int begin,int end) { if(sequence.empty()||begin>end)//是否需要判断begin>end return false; int i=begin; for(;i<end;i++) { if(sequence[i]>sequence[end]) { break; } } for(int j = i;j<end;j++) { if(sequence[j]<sequence[end]) { return false; } } bool left = true; if(i>begin) { left = bst(sequence,begin,i-1); } bool right = true; if(i<end-1) { right =bst(sequence,i,end-1); } return left&right; } };