玩转算法面试(四)栈和队列(Java)

栈和队列

一、栈

1、简单的栈运用

20. 有效的括号

给定一个字符串,只包含(,[,{,),],}, 判定字符串中的括号匹配是否合法。

  • 如"()”,“()[{"是合法的
  • 如“(]","([)]" 是非法的
class Solution {
   
    public boolean isValid(String s) {
   
        Stack<Character> stack = new Stack<>();
        for(int i = 0;i < s.length();i++) {
   
            char t = s.charAt(i);
            if(t == '[' || t == '{' || t == '(' ) {
   
                stack.push(t);
            }
            else {
   
            if(stack.isEmpty()) return false;
            if(t == ')') {
   
                if(stack.isEmpty()) return false;
                if(stack.peek() == '(') stack.pop();
                else return false;
            }
            if(t == '}') {
   
    
                if(stack.peek() == '{') stack.pop();
                else return false;
            }
            if(t == ']') {
   

                if(stack.peek() == '[') stack.pop();
                else return false;
            }
        }
        }

        if(!stack.isEmpty()) return false;
        
        return true;
    }
}

拓展:

  1. Evaluate Reverse Polish Notation

  2. Simplify Path

2、栈和递归的紧密关系

2.1、递归算法
二叉树中的算法

前序、中序和后序遍历

  1. Binary Tree Preorder Traversal

image.png

image.png

  1. Binary Tree Inorder Traversal

  2. Binary Tree Postorder Traversal

2.2、使用栈模拟系统栈,写出非递归程序
LeetCode 144. 二叉树前序遍历

image.png

image.png

public class Solution144 {
   

    // Definition for a binary tree node.
    public class TreeNode {
   
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode(int x) {
    val = x; }
    }

    private class Command{
   
        String s;   // go, print
        TreeNode node;
        Command(String s, TreeNode node){
   
            this.s = s;
            this.node = node;
        }
    };

    public List<Integer> preorderTraversal(TreeNode root) {
   

        ArrayList<Integer> res = new ArrayList<Integer>();
        if(root == null)
            return res;

        Stack<Command> stack = new Stack<Command>();
        stack.push(new Command("go", root));
        while(!stack.empty()){
   
            Command command = stack.pop();

            if(command.s.equals("print"))
                res.add(command.node.val);
            else{
   
                assert command.s.equals("go");
                if(command.node.right != null)
                    stack.push(new Command("go",command.node.right));
                if(command.node.left != null)
                    stack.push(new Command("go",command.node.left));
                stack.push(new Command("print", command.node));
            }
        }
        return res;
    }

}

public class Solution1 {
   

    public List<Integer> preorderTraversal(TreeNode root) {
   

        ArrayList<Integer> res = new ArrayList
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值