leetcode-剑指33-OK

// language c
// 剑指33
// https://leetcode-cn.com/problems/er-cha-sou-suo-shu-de-hou-xu-bian-li-xu-lie-lcof/


bool verifyPostorder(int* postorder, int postorderSize){
    if(postorderSize==0)
    return true;
	bool inside(int * NeedToBeJudged, int start, int end){
		if(start==end)
			return true;
		int mid = NeedToBeJudged[end];
		int p = start;
		while(NeedToBeJudged[p]<mid)	// 循环完后p指向第一个大于等于mid的值
			p++;
		bool left =true;
		for(int i =p;i<end;i++){
			if(NeedToBeJudged[i]<=mid){
				return false;
			}
		}
		bool right = true;
		if(p>start)
			left = inside(NeedToBeJudged,start,p-1);
		if(p<end)
			right = inside(NeedToBeJudged,p,end-1);
		return left&&right;
	}

	return inside(postorder,0,postorderSize-1);
}
posted @ 2021-01-31 19:06  RougeBW  阅读(34)  评论(0编辑  收藏  举报