Stack_Queue_OJ题

1.逆波兰表达式

#include <iostream>
#include <vector>
#include <string>
#include <stack>
using namespace std;
int evalRPN(vector<string>& tokens) {
	stack<int> num;
	for (size_t i = 0; i < tokens.size(); i++){
		string& str = tokens[i];
		if (!("+" == str || "-" == str || "*" == str || "/" == str)){
			num.push(atoi(str.c_str()));          //将数字入栈
		}
		else{
			int right = num.top();
			num.pop();
			int left = num.top();
			num.pop();
			switch (str[0])
			{
			case '+':
				num.push(left + right);
				break;
			case '-':
				num.push(left - right);
				break;
			case '*':
				num.push(left*right);
				break;
			case '/':
				num.push(left / right);
				break;
			}
		}
	}
	return num.top();
}
int main()
{
	vector<string> s; //["2", "1", "+", "3", "*"]
	s.push_back("2");
	s.push_back("1");
	s.push_back("+");
	s.push_back("3");
	s.push_back("*");
	cout << evalRPN(s) << endl;
	return 0;
}

2.前序遍历二叉树

#include <iostream>
#include <vector>
#include <string>
#include <stack>
using namespace std;
typedef struct TreeNode {
	int val;
	TreeNode *left;
	TreeNode *right;
	TreeNode(int x) : val(x), left(NULL), right(NULL) {}
}Tree;
//vector<int> preorderTraversal(TreeNode* root)
//{
//	vector<int> ret;
//	vector<int> tmp;
//	if (root == nullptr)
//		return ret;
//	ret.push_back(root->val);
//	tmp = preorderTraversal(root->left);
//	for (size_t i = 0; i < tmp.size(); i++){
//		ret.push_back(tmp[i]);
//	}
//	tmp = preorderTraversal(root->right);
//	for (size_t i = 0; i < tmp.size(); i++){
//		ret.push_back(tmp[i]);
//	}
//	return ret;
//}
vector<int> preorderTraversal(TreeNode* root) {
	vector<int> ret;
	stack<TreeNode*> node;
	if (root == nullptr){
		return ret;
	}
	TreeNode* cur = root;
	while (cur || !node.empty())
	{
		while (cur){
			node.push(cur);
			ret.push_back(cur->val);
			cur = cur->left;
		}
		cur = node.top()->right;
		node.pop();
	}
	return ret;
}
int main()
{
	Tree* root = new TreeNode(1);
	root->left = nullptr;
	root->right = new TreeNode(2);
	root->right->left = new TreeNode(3);
	vector<int> ret = preorderTraversal(root);
	for (auto x : ret){
		cout << x << " ";
	}
	return 0;
}

3.后续遍历二叉树

#include <iostream>
#include <vector>
#include <string>
#include <stack>
using namespace std;
typedef struct TreeNode {
	int val;
	TreeNode *left;
	TreeNode *right;
	TreeNode(int x) : val(x), left(NULL), right(NULL) {}
}Tree;
vector<int> postorderTraversal(TreeNode* root) {
	vector<int> ret;
	stack<TreeNode*> node;
	TreeNode* prev = nullptr;
	if (root == nullptr){
		return ret;
	}
	TreeNode* cur = root;
	while (cur || !node.empty()){
		while (cur){
			node.push(cur);
			cur = cur->left;
		}
		TreeNode* top = node.top();
		if (top->right == nullptr || top->right == prev)   //一旦右子树访问过了
		{
			ret.push_back(top->val);
			prev = top;
			node.pop();
		}
		else
		{
			cur = top->right;
		}
	}
	return ret;
}
int main()
{
	Tree* root = new TreeNode(1);
	root->left = nullptr;
	root->right = new TreeNode(2);
	//root->right->left = new TreeNode(3);
	vector<int> ret = postorderTraversal(root);
	for (auto x : ret){
		cout << x << " ";
	}
	return 0;
}

4.层序遍历二叉树,倒序输出

#include <iostream>
#include <vector>
#include <string>
#include <queue>
using namespace std;
typedef struct TreeNode {
	int val;
	TreeNode *left;
	TreeNode *right;
	TreeNode(int x) : val(x), left(NULL), right(NULL) {}
}Tree;

vector<vector<int>> levelOrder(TreeNode* root) {
	vector<vector<int>> vv;
	queue<TreeNode*> q;
	if (root == nullptr)
		return vv;
	q.push(root);
	while (!q.empty()){
		int sz = q.size();      //sz为每层结点的个数
		vector<int> v;
		while (sz--){
			TreeNode* top = q.front();
			v.push_back(top->val);
			q.pop();
			if (top->left)
				q.push(top->left);
			if (top->right)
				q.push(top->right);
		}
		vv.push_back(v);
	}
	for (int i = 0; i<vv.size() / 2; i++){     //交换元素,把最底层的元素放在第一层
		swap(vv[i], vv[vv.size() - 1 - i]);
	}
	return vv;
}

5.层序遍历N叉数

#include <iostream>
#include <vector>
#include <string>
#include <queue>
using namespace std;
class Node {
public:
	int val = NULL;
	vector<Node*> children;
	Node() {}
	Node(int _val, vector<Node*> _children) {
		val = _val;
		children = _children;
	}
};

vector<vector<int>> levelOrder(Node* root) {
	vector<vector<int>> vv;
	if (root == nullptr){
		return vv;
	}
	queue<Node*> q;
	q.push(root);
	while (!q.empty()){
		int sz = q.size();
		vector<int> v;
		while (sz--){
			Node* top = q.front();
			v.push_back(top->val);
			q.pop();
			if (!top->children.empty()){
				for (int i = 0; i<top->children.size(); i++){      //遍历Node里边的children
					q.push(top->children[i]);
				}
			}
		}
		vv.push_back(v);
	}
	return vv;
}

6.二叉树最近的公共祖先节点

#include <iostream>
#include <vector>
#include <string>
#include <stack>
using namespace std;
typedef struct TreeNode {
	int val;
	TreeNode *left;
	TreeNode *right;
	TreeNode(int x) : val(x), left(NULL), right(NULL) {}
}Tree;


bool FindNode(TreeNode* root, TreeNode* node)    //在以root为根的树中找node
{
	if (root == nullptr){
		return false;
	}
	if (root == node){
		return true;
	}
	return FindNode(root->left, node) || FindNode(root->right, node);
}
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q)
{
	if (root == nullptr){
		return root;
	}
	if (root == p || root == q)
		return root;
	bool lp = FindNode(root->left, p);      //在左右子树中分别找p,q
	bool lq = FindNode(root->left, q);
	bool rp = FindNode(root->right, p);
	bool rq = FindNode(root->right, q);
	if (lp && lq)            //若都在左子树,那在左子树中继续找
		return lowestCommonAncestor(root->left, p, q);
	if (rp && rq)            //若都在右子树,那在右子树中继续找
		return lowestCommonAncestor(root->right, p, q);
	if ((lp&&rq) || (rp&&lq))
		return root;
	else
		return nullptr;
}

7.移除数组中指定的元素

#include <iostream>
#include <vector>
using namespace std;

//1.遍历数组,直到找到要删的元素
//2.从该元素的后边找到别的元素,交换
//3.当j遍历完的时候,就结束了
//4.再遍历到要删的元素,截断
int removeElement(vector<int>& nums, int val) {
	size_t sz = nums.size();
	size_t i, j;
	for ( i = 0; i<sz; i++){
		if (val == nums[i]){              
			for (j = i + 1; j<sz; j++){
				if (nums[j] != nums[i]){
					swap(nums[i], nums[j]);
					break;
				}
			}
		}
		if (j == sz){
			break;
		}
	}
	for (i = 0; i<sz; i++)
	{
		if (nums[i] == val){
			nums.resize(i);
			break;
		}
	}
	return i;
}

int main()
{
	vector<int> nums;
	nums.push_back(3);
	nums.push_back(2);
	nums.push_back(2);
	nums.push_back(3);
	cout << removeElement(nums, 3);
}

8.两个栈实现一个队列

#include <iostream>
#include <stack>
using namespace std;
template<class T>
class Queue
{
private:
	stack<T> s1;
	stack<T> s2;
public:
	void Push(T data)     //将数据压入栈1
	{
		s1.push(data);
	}
	void Pop()
	{
		if (s1.empty() && s2.empty())
			cout << "无元素!" << endl;
		if (!s2.empty())              //从栈2出
		{
			s2.pop();
		}
		else
		{
			while (!s1.empty()){
				s2.push(s1.top());
				s1.pop();
			}
			s2.pop();
		}
	}
	T& top()
	{
		if (s1.empty() && s2.empty())
			return nullptr;
		if (s2.empty()){
			while (!s1.empty()){
				s2.push(s1.top());
				s1.pop();
			}
		}
		return s2.top();
	}
	bool Empty()
	{
		if (s1.empty() && s2.empty())
			return true;
		else
			return false;
	}
	void Print()
	{
		if (s1.empty() && s2.empty())
		{
			cout << "无元素!" << endl;
			return;
		}
		if (s2.empty())
		{
			while (!s1.empty()){
				s2.push(s1.top());
				s1.pop();
			}
		}
		if (!s2.empty())
		{
			while (!s2.empty()){
				cout << s2.top() << endl;
				s2.pop();
			}
			if (!s1.empty())
			{
				while (!s1.empty())
				{
					s2.push(s1.top());
					s1.pop();
				}
				while (!s2.empty())
				{
					cout << s2.top() << endl;
					s2.pop();
				}
			}
		}
	}
};

int main()
{
	Queue<int> q1;
	q1.Push(1);
	q1.Push(2);
	q1.Push(3);
	q1.Pop();
	q1.Pop();
	q1.Push(4);
	q1.Print();
	return 0;
}

9.两个队列实现一个栈

#include <iostream>
#include <vector>
#include <string>
#include <queue>
using namespace std;
class MyStack {
	queue<int> q1;
	queue<int> q2;
public:
	void push(int x) {
		if (!q2.empty()){
			q2.push(x);
			return;
		}
		q1.push(x);
	}

	/** Removes the element on top of the stack and returns that element. */
	int pop() {                     //始终保持一个队列为空
		if (!q1.empty()){
			int sz = q1.size();
			for (size_t i = 0; i < sz - 1; i++){
				q2.push(q1.front());
				q1.pop();
			}
			int ret = q1.front();
			q1.pop();
			return ret;
		}
		if (!q2.empty()){
			int sz = q2.size();
			for (size_t i = 0; i < sz - 1; i++){
				q1.push(q2.front());
				q2.pop();
			}
			int ret = q2.front();
			q2.pop();
			return ret;
		}
		else
			return NULL;
	}

	/** Get the top element. */
	int top() {
		if (!q1.empty()){
			int sz = q1.size();
			for (size_t i = 0; i < sz - 1; i++){
				q2.push(q1.front());
				q1.pop();
			}
			int ret = q1.front();
			q2.push(ret);
			q1.pop();
			return ret;
		}
		if (!q2.empty()){
			int sz = q2.size();
			for (size_t i = 0; i < sz - 1; i++){
				q1.push(q2.front());
				q2.pop();
			}
			int ret = q2.front();
			q1.push(ret);
			q1.pop();
			return ret;
		}
		else
			return NULL;
	}

	/** Returns whether the stack is empty. */
	bool empty() {
		if (q1.empty() && q2.empty())
			return true;
		return false;
	}
};

int main()
{
	MyStack m;
	m.push(1);
	m.push(2);
	m.push(3);
	cout << m.top() << endl;
	return 0;
}

10.最小栈

#include <iostream>
#include <stack>
#include <assert.h>
#include <vector>
using namespace std;

class minStack
{
private:
	stack<int> _cur;
	stack<int> _min;
	vector<int> _arr;
public:
	void Push(int x)
	{
		_cur.push(x);
		if (_min.empty())
		{
			_min.push(x);
			_arr.push_back(1);
			return;
		}
		if (x < _min.top())
		{
			_min.push(x);
			_arr.push_back(1);
			return;
		}
		if (x == _min.top())
		{
			_arr[_arr.size() - 1]++;
		}
	}
	void Pop()
	{
		assert(!_cur.empty());
		if (_cur.top() == _min.top())
		{
			if (_arr[_arr.size() - 1] == 1)
			{
				_cur.pop();
				_min.pop();
			}
			else
			{
				_cur.pop();
				_arr[_arr.size() - 1]--;
			}
		}
		else
		{
			_cur.pop();
		}
	}
	int Top()
	{
		return _cur.top();
	}
	int GetMin()
	{
		return _min.top();
	}
};

int main()
{
	minStack min;
	min.Push(1);
	min.Push(8);
	min.Push(0);
	min.Push(0);
	min.Push(0);
	min.Push(3);
	min.Pop();
	min.Pop();
	min.Pop();
	min.Pop();
	cout << min.Top() <<endl;
	cout << min.GetMin() << endl;
	return 0;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值