Leetcode解题笔记(Stack)
* 源码见github https://github.com/Kelvinmao/Leetcode/tree/master/Stack *
2016-07-22更新
94.Binary Tree Inorder Traversal
Given a binary tree, return the inorder traversal of its nodes’ values.
For example:
Given binary tree [1,null,2,3],
return [1,3,2].
题目要求完成二叉树中序遍历,递归解法太简单我们不考虑。非递归解法在之前的博文里有过详细介绍,主要的思路是:用一个栈存放指向树结点的指针,一个vector用于
存放值,一个pCur指向根节点;当pCur有左子树时,pCur->left进栈,pCur=pCur->left;若pCur没有左子树,则直接将pCur->c
al放进vector,并且pCur向右子树移动,若没有右子树,则pCur回退到上一节点也就是pCur=stack.top(),并且将上一节点的值放入list
,同时上一节点出栈并且pCur向右子树移动。代码如下:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int> list;
vector<int> inorderTraversal(TreeNode* root) {
if(!root)
return list;
stack<struct TreeNode *> S;
struct TreeNode * pCur=root;
while(pCur||!S.empty()){
if(pCur->left){
S.push(pCur);
pCur=pCur->left;
}
else{
list.push_back(pCur->val);
pCur=pCur->right;
while(!pCur&&!S.empty()){
pCur=S.top();
list.push_back(pCur->val);
S.pop();
pCur=pCur->right;
}
}
}
return list;
}
};
144.Binary Tree Preorder Traversal Given a binary tree, return the preorder
traversal of its nodes’ values. For example: Given binary tree {1,#,2,3}
return [1,2,3]. 题目要求进行前序遍历,也在之前的博文中详细讲过,主要思路是,将pCur的值放入vector,同时pCur进栈,pCur向左子
树移动;没有左子树则出栈并向右子树移动,具体请看代码:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int>list;
vector<int> preorderTraversal(TreeNode* root) {
if(!root)
return list;
stack<struct TreeNode *> S;
struct TreeNode * pCur=root;
while(pCur||!S.empty()){
list.push_back(pCur->val);
S.push(pCur);
pCur=pCur->left;
while(!pCur&&!S.empty()){
pCur=S.top();
S.pop();
pCur=pCur->right;
}
}
return list;
}
};
2016-07-20更新
20.Valid Parentheses Given a string containing just the characters ’(‘, ‘)’,
determine if the input string is valid. The brackets
‘{‘, ‘}’, ‘[’ and ‘]’,
must close in the correct order,“()”
and ”()[]{}”
are all valid but
and
“(]”“([)]”
are not.
括号匹配问题,在之前的博客上也有写过,不过这次遇到了应该是leetcode上的一个坑,各位在代码注释里会看到。对于这个问题,我们利用栈的思想来解决,出栈条件
是: * 当字符串中的符号是左括号时,如果此时栈顶元素是与之匹配的右括号,则出栈;否则一律入栈。把握好这一点即可。 * 代码见github。
232.Implement Queue using Stacks
Implement the following operations of a queue using stacks.
本题要求用栈来模拟队列,这道题曾经是我的数据结构期中考试题,当时我的做法是用两个栈,一个输入栈和另一个输出栈。要入栈,直接push进输入栈,要出栈,则先出输
入栈再入输出栈再出输出栈即可。在参考了讨论区之后,又有了这样的算法:定义stack结构中含有int *arr和int
top两个元素,arr是动态分配数组,top为栈顶指针,每次入栈,将top
后移,同时将所有元素向后移动一位,置空第一个位置,将新元素放在第一个位置即可。出栈时,top–即可。代码见github。
225.Implement Stack using Queues
Implement the following operations of a stack using queues.
- push(x) – Push element x onto stack.
- pop() – Removes the element on top of the stack.
- top() – Get the top element.
- empty() – Return whether the stack is empty.
Notes:
You must use only standard operations of a queue – which means only push to
back, peek/pop from front, size, and is empty operations are valid.
Depending on your language, queue may not be supported natively. You may
simulate a queue by using a list or deque (double-ended queue), as long as you
use only standard operations of a queue.
You may assume that all operations are valid (for example, no pop or top
operations will be called on an empty stack).
题意是用队列来模拟栈,思路和上面的题差不多,直接看代码吧。