标签:out clu end 剑指offer csharp turn std node har
/* 题目: 定义栈的数据结构,实现一个能够得到栈的最小元素的min函数。 */ /* 思路: 错误思路:每次保存当前最小的元素,如果当前最小元素弹出,最小元素是谁? 正确思路:两个栈,一个栈保存数据,另一个栈用于存储当前最小元素。 */ #include <iostream> #include<cstdlib> #include<stack> using namespace std; stack<int> m_data; stack<int> m_min; void push(int value) { m_data.push(value); int minVal = value; if(!m_min.empty()){ int temp = m_min.top(); if(temp < value){ minVal = temp; } } m_min.push(minVal); } void pop() { if(!m_data.empty()){ m_data.pop(); m_min.pop(); } } int top() { return m_data.top(); } int min() { return m_min.top(); } int main() { /* TreeNode *node7 = new TreeNode(7); TreeNode *node6 = new TreeNode(4); TreeNode *node5 = new TreeNode(3); TreeNode *node4 = new TreeNode(9); TreeNode *node3 = new TreeNode(7); TreeNode *node2 = new TreeNode(8); TreeNode *node1 = new TreeNode(8); node1->left = node2; node1->right = node3; node2->left = node4; node2->right = node5; node5->left = node6; node5->right =node7; TreeNode *node31 = new TreeNode(2); TreeNode *node21 = new TreeNode(9); TreeNode *node11 = new TreeNode(8); node11->left = node21; node11->right = node31; cout<<Mirror(node1)<<endl;*/ return 0; }
标签:out clu end 剑指offer csharp turn std node har
原文地址:https://www.cnblogs.com/buaaZhhx/p/11931554.html