二叉树的镜像、顺时针打印矩阵、包含min函数的栈(剑指offer18-20)c++版本

转载请注明出处。

#include <iostream>
#include <vector>
#include <stack>

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:
	//JZ18 二叉树的镜像
	void Mirror(TreeNode* pRoot);
	TreeNode* MirrorCore(TreeNode* pRoot);
	//JZ19 顺时针打印矩阵
	vector<int> printMatrix(vector<vector<int> > matrix);
	//JZ20 包含min函数的栈
	int MAX = 0;
	stack<int> s;
	stack<int> smin;
	void push(int value);
	void pop();
	int top();
	int min();
};	

void Solution::Mirror(TreeNode* pRoot) {
	//递归求解
	if (!pRoot)	return;
	pRoot = MirrorCore(pRoot);
	return;
}
TreeNode* Solution::MirrorCore(TreeNode* pRoot) {
	if (!pRoot)
		return nullptr;
	TreeNode* temp = pRoot->left;
	pRoot->left = MirrorCore(pRoot->right);
	pRoot->right = MirrorCore(temp);
	return pRoot;
}
vector<int> Solution::printMatrix(vector<vector<int> > matrix) {
	if (matrix.empty())
		return vector<int>();
	vector<int> result;
	int rows = matrix.size();
	int cols = matrix[0].size();
	int left = 0, right = cols - 1, up = 0, down = rows - 1;
	while (left < right && up < down) {
		for (int i = left; i < right; i++) {
			result.push_back(matrix[up][i]);
		}
		for (int i = up; i < down; i++) {
			result.push_back(matrix[i][right]);
		}
		for (int i = right; i > left; i--) {
			result.push_back(matrix[down][i]);
		}
		for (int i = down; i > up; i--) {
			result.push_back(matrix[i][left]);
		}
		up++, down--, left++, right--;
	}
	if (up == down && left <= right) {
		for (int i = left; i <= right; i++)
			result.push_back(matrix[up][i]);
	}
	if (left == right && up < down) {
		for (int i = up; i <= down; i++) {
			result.push_back(matrix[i][left]);
		}
	}
	return result;
}
void Solution::push(int value) {
	s.push(value);
	if (smin.empty() || smin.top() > s.top())	smin.push(value);
	else
		smin.push(smin.top());//保证当出栈一个元素,smin 的栈顶还是最小值
	return;
}
void Solution::pop() {
	if (s.empty() || smin.empty())	return;
	s.pop(); smin.pop();
	return;
}
int Solution::top() {
	if (s.empty() || smin.empty()) {
		MAX = -1;
		return 0;
	}
	return s.top();
}
int Solution::min() {
	if (s.empty() || smin.empty()) {
		MAX = -1;
		return 0;
	}
	return smin.top();
}

void test1() {
	//JZ18
	TreeNode* pRoot = new TreeNode(1);
	pRoot->left = new TreeNode(2);
	pRoot->right = new TreeNode(3);
	pRoot->left->left = new TreeNode(4);
	pRoot->left->right = new TreeNode(5);
	Solution s;
	s.Mirror(pRoot);
	TreeNode* pNode = pRoot;
	while (pNode) {
		cout << pNode->val << endl;
		pNode = pNode->right;
	}
	return;
} 
void test2() {
	//JZ19
	//vector<vector<int> > temp = { {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12} };
	vector<vector<int> > temp = { {1} };
	Solution s;
	vector<int> result = s.printMatrix(temp);
	vector<int>::iterator it;
	for (it = result.begin(); it != result.end(); it++) {
		cout << *it << ',';
	}
	return;
}
void test3() {
	Solution sol;
	sol.push(1);
	sol.push(2);
	int result = sol.min();
	cout << result << endl;
	sol.push(0);
	result = sol.min();
	cout << result << endl;
	sol.pop();
	result = sol.min();
	cout << result << endl;
	return;
}

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值