转载请注明出处。
#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;
}