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->cal放进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 `’(‘, ‘)’, ‘{‘, ‘}’, ‘[’ and ‘]’,` determine if the input string is valid. The brackets must close in the correct order,` “()”` and `”()[]{}” `are all valid but` “(]”` and` “([)]”` are not.

括号匹配问题,在之前的博客上也有写过,不过这次遇到了应该是leetcode上的一个坑,各位在代码注释里会看到。对于这个问题,我们利用栈的思想来解决,出栈条件是:当字符串中的符号是左括号时,如果此时栈顶元素是与之匹配的右括号,则出栈;否则一律入栈。把握好这一点即可。代码见github。

Leetcode20题



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。

Leetcode232题

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).

题意是用队列来模拟栈,思路和上面的题差不多,直接看代码吧。

Leetcode225

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值