栈的压入、弹出序列,从上往下打印二叉树,二叉搜索树的后序遍历序列(剑指offer21-23)c++版本

#include 
#include 
#include 
#include 

using namespace std;

struct TreeNode {
	int val;
	struct TreeNode* left;
	struct TreeNode* right;
	TreeNode(int x) :
		val(x), left(NULL), right(NULL) {
	}
};

class Solution {
public:
	//JZ21 栈的压入、弹出序列
	bool IsPopOrder(vector<int> pushV, vector<int> popV);
	//JZ22 从上往下打印二叉树
	vector<int> PrintFromTopToBottom(TreeNode* root);
	//JZ23 二叉搜索树的后序遍历序列
	bool VerifySquenceOfBST(vector<int> sequence);
	bool VerifySquenceOfBSTcore(vector<int> sequence, int left, int right);
};
//JZ21
bool Solution::IsPopOrder(vector<int> pushV, vector<int> popV) {
	int len1 = pushV.size(); 
	int len2 = popV.size();
	if (len1 == 0 || len1 != len2)	return false;
	int idpop = 0, idpush = 0;
	stack<int> temp;
	while(idpop < len1) {
		while (temp.empty() || temp.top() != popV[idpop]) {
			temp.push(pushV[idpush++]);
			if (idpush >= len1 && temp.top() != popV[idpop])
				//此时,表明两个vector中的元素不相同
				return false;
		}
		temp.pop();
		idpop++;
	}
	if (temp.empty())	return true;
	else
		return false;
}
//JZ22
vector<int> Solution::PrintFromTopToBottom(TreeNode* root) {
	//维持一个队列,出队当前层的结点,出队下一层的结点
	if (!root)	return vector<int>();
	queue<TreeNode*> q;
	vector<int> result;
	q.push(root);
	int q_cur = 1;//记录当前层的结点数
	int q_next = 0;//记录下层结点数
	while (!q.empty()) {
		while (q_cur) {
			result.push_back(q.front()->val);
			if (q.front()->left) {
				q.push(q.front()->left);
				q_next++;
			}
			if (q.front()->right) {
				q.push(q.front()->right);
				q_next++;
			}
			q.pop();
			q_cur--;
		}
		q_cur = q_next;
		q_next = 0;
	}
	return result;
}
//JZ23
bool Solution::VerifySquenceOfBST(vector<int> sequence) {
	if (sequence.empty())	return false;
	int len = sequence.size();
	int left = 0, right = len - 1;
	return VerifySquenceOfBSTcore(sequence, left, right);
}
bool Solution::VerifySquenceOfBSTcore(vector<int> sequence, int left, int right) {
	if (left == right)	return true;
	int leftcount = left;//记录左子树中的结点	
	while (sequence[leftcount] < sequence[right])	leftcount++;
	int i = leftcount;
	while (i < right) {
		if (sequence[i] < sequence[right])//右子树中的结点必定大于根节点
			return false;
		i++;
	}
	if (leftcount == left || leftcount == right)	return VerifySquenceOfBSTcore(sequence, left, right - 1);
	else return VerifySquenceOfBSTcore(sequence, left, leftcount-1) && VerifySquenceOfBSTcore(sequence, leftcount, right - 1);
}

//JZ21
void test1() {
	vector<int> pushV = { 1,2,3,4,5 };
	vector<int> popV = { 4,6,3,2,1 };
	Solution s;
	int result = 0;
	if (s.IsPopOrder(pushV, popV))
		result = 1;
	cout << result;
	return;
}
//JZ22
void test2() {
	TreeNode* root = new TreeNode(1);
	root->left = new TreeNode(2);
	root->right = new TreeNode(3);
	root->left->left = new TreeNode(4);
	root->left->right = new TreeNode(5);
	root->right->left = new TreeNode(6);
	Solution s;
	vector<int> result = s.PrintFromTopToBottom(root);
	vector<int>::iterator it = result.begin();
	for (it; it != result.end(); it++)
		cout << *it << ' ';
	return;
}
//JZ23
void test3() {
	//vector test = { 2, 4, 3, 7, 6, 5 };
	vector<int> test = { 2,3,6,4,7,5 };
	Solution s;
	cout << (int)s.VerifySquenceOfBST(test);
	return;
}

int main() {
	//test1();
	//test2();
	test3();
	system("pause");
	return 0;
}

你可能感兴趣的:(剑指offer,算法,算法,c++)