ACM算法笔记(四)栈、队列、二叉树的遍历

news/2024/9/19 11:57:54

一、用栈实现队列

        用两个栈实现队列(先进先出),队列应当支持(push、pop、peek、empty)

                思路:双栈一个负责输入一个负责输出(压入操作正常,弹出时将输入栈的元素依次弹出然后压入输出栈

private static Stack<Integer> inStack;    //输入栈
private static Stack<Integer> outStack;   //输出栈

public void push(int x);
{
    inStack.push(x);
}

public int pop()    
{
    if(outStack.isEmpty())
        in2Out();
    return outStack.pop();
}

public int peek()
{
    if(outStack.isEmpty())
        in2Out();
    return outStack.peek();
}

public bool empty()
{
    return inStack.isEmpty() && outStack.IsEmpty();
}

public void in2Out()    //循环弹栈后压栈
{
    while(!inStack.isEmpty())
        outStack.push(inStack.pop());
}

public int peek()
{
    if(outStack.isEmpty())
        in2Out();
    return ouStack.peek();
}

二、字符串解码

                 思路:遇到 ] 时,表示其前方的字符串需要重复(截止到 [ ),重复次数为 [ 前的(最近的)数字。所以我们需要按顺序回溯之前的字符串-->使用栈进行处理

                ①将数据压栈直到遇到 ] 为止

                ②开始出栈,直到遇到第一个 [ 前一个位置

                ③将已出栈的元素刨去 [ ] 组成一个临时字符串(可能是多位数字的时候需要单独处理),重复n次

                ④将重复后的临时字符串再次压栈,然后继续步骤①

int ptr;

public String decodeString(String s)
{
    LinkedList<String> stk = new LinkedList<string>();
    ptr = 0;

    while(ptr < s.length())
    {
        char cur = s.charAt(ptr);
        if(Character.isDigit(cur))
        {
            String digits = getDigits(s);    //单独处理数字
            stk.addLast(digits);
        }

        else if(Caracter.isLetter(cur) || cur == '[')    //处理普通字符/[
            stk.addLast(String.valueOf(s.charAt(ptr++)));

        else    //处理]
        {
            ++ptr;
            LinkedList<String> sub = new LinkedList<String>();    //使用另一个list处理字符串
            while(!'['.equals(stk.peekLast()))
                sub.addLast(stk.removeLast());

            Collections.reverse(sub);//倒置一次后出栈
            stk.removeLast();

            int repTime = Integer.parseInt(stk.removeLast());
            StringBuffer t = new StringBuffer();
            String o = getString(sub);
            while(repTime-- > 0)
                t.append(o);    //将临时字符串重复repTime遍

            stk.addLast(t.toString());    //将构造好的字符串入栈
        }
    }
    return geString(stk);
}

三、二叉树遍历

        1、中序遍历

                思路:从根结点开始,先遍历左子树,后遍历右子树(表现顺序为:左->根->右)

                 遍历顺序G D H B A E I C F

public void accessTree(TreeNode root, List<Integer> res)
{
    if(root == null)
        return;
    //遍历顺序:左->根->右
    accessTree(root.left,res);
    res.add(root.val);
    accessTree(root.right,res);
}

                思路2:利用栈实现循环访问

                        ①从根结点开始依次访问左子树,若此节点的左子树不为空则将此节点压入栈中

                        ②依次弹栈,若该节点无右子树则直接加入队列(访问结果);若其有右子树则先将该节点入队,再将其右子树压栈

                        ③重复步骤②

public List<int> inorderTraversalWithLoop(TreeNode root)
{
    List<int> res = new ArrayList<int>();
    Deque <TreeNode> stack = new LinkList<TreeNode>();
    while(root != null || stack.isEmpty())
    {
        while(root != null)
        {
            stack.push(root);
            root = root.left;
        }
        root = stack.pop();
        res.add(root.val);
        root = root.right;
    }
    return res;
}

        2、前序遍历

                思路:输出的时候先输出根结点,再输出左子树,随后输出右子树

public void accessTree(TreeNode root, List<Integer> res)
{
    if(root == null)
        return;
    //遍历顺序:根->左->右
    res.add(root.val);    //输出语句前移
    accessTree(root.left,res);
    accessTree(root.right,res);
}

                思路2:同中序遍历,仅需将输出语句嵌入while循环内部即可

public List<int> inorderTraversalWithLoop(TreeNode root)
{
    List<int> res = new ArrayList<int>();
    Deque <TreeNode> stack = new LinkList<TreeNode>();
    while(root != null || stack.isEmpty())
    {
        while(root != null)
        {
            res.add(root.val);
            stack.push(root);
            root = root.left;
        }
        root = stack.pop();
        root = root.right;
    }
    return res;
}

        3、后序遍历

                 思路:输出的时候先输出左子树,再输出右子树,随后输出根结点

public void accessTree(TreeNode root, List<Integer> res)
{
    if(root == null)
        return;
    //遍历顺序:根->左->右
    accessTree(root.left,res);
    accessTree(root.right,res);
    res.add(root.val);    //输出语句后移
}

                 思路2:在中序遍历的基础上,输出左子树后需要输出右子树再输出根结点。代码需要进行一定的调整(经过根结点到达右子树)

public List<int> inorderTraversalWithLoop(TreeNode root)
{
    List<int> res = new ArrayList<int>();
    Deque <TreeNode> stack = new LinkList<TreeNode>();

    TreeNode prevAccess = null;

    while(root != null || stack.isEmpty())
    {
        while(root != null)
        {
            res.add(root.val);
            stack.push(root);
            root = root.left;
        }
        if(root.right == null || root.rigth == prevAccess)    //右子树被访问过
        {
            root = stack.pop();
            prevAccess = root;
            root = null;
        }
        else
        {
            stack.push(root);
            root = root.right;
        }
    }
    return res;
}


http://www.niftyadmin.cn/n/4217500.html

相关文章

java开发为什么需要UML

出处 www.cnejb.com 知道UML造成了怎样的局面大混乱吗&#xff1f;知道什么样的功能是UML拥有但JAVA不具备的吗&#xff1f;知道我们为什么需要除JAVA外的另一种电脑语言吗&#xff1f;UML并不仅仅只是JAVA或者其它什么语言的替代品。UML并不仅仅只是JAVA或者其它什么语言的替代…

ACM算法笔记(五)二叉树的检查和变换

一、对称二叉树 给定一颗二叉树&#xff0c;检查他是否对称 思路&#xff1a;使用递归处理&#xff08;递归的比较左子树的左孩子和右子树的右孩子&#xff09; public bool isSymmetric(TreeNHode root) {if(root nuill)return ture;return deepCheck(root.left,root.right)…

对Vue的生命周期的理解

因为是第一天学习vue&#xff0c;可能有理解不全面或者错误的地方&#xff0c;如果有误导&#xff0c;非常抱歉。 Vue的生命周期1. 生命周期是什么2. 生命周期过程图3. Vue的生命周期钩子函数4. 测试钩子函数1. 生命周期是什么 Vue的每个组件都是独立的&#xff0c;从一个组件…

Vue学习Day2 v-bind动态绑定属性、计算属性、v-on事件监听、v-if、登陆切换小案例、v-show

想利用暑假时间好好学习一下vue&#xff0c;会记录每一天的学习内容。 今天是学习vue的第2天&#xff01; 起起伏伏乃人生常态&#xff0c;继续加油&#xff5e; 学习内容1. v-bind 使用简单使用v-bindv-bind语法糖v-bind动态绑定class对象语法v-bind动态绑定class数组语法v-bi…

Vue学习Day3 v-for遍历、组件的key属性、列表点击变色案例、书籍购物车案例

想利用暑假时间好好学习一下vue&#xff0c;会记录每一天的学习内容。 今天是学习vue的第3天&#xff01; 起起伏伏乃人生常态&#xff0c;继续加油&#xff5e; 学习内容1. v-for使用v-for遍历数组v-for遍历对象组件的key属性2.检测数组更新3.列表项点击变色简单案例4. 书籍购…

Vue学习Day4 v-model表单双向绑定、注册组件、组件模版分离、组件中的数据存放、组件中data为什么是个函数

想利用暑假时间好好学习一下vue&#xff0c;会记录每一天的学习内容。 今天是学习vue的第4天&#xff01; 起起伏伏乃人生常态&#xff0c;继续加油&#xff5e; 学习内容1. 表单绑定v-modelv-model原理v-model&#xff1a;radiov-model&#xff1a;checkboxv-model&#xff1a…

Myeclipse打开就出错Could not create the Java virtual machine

Myeclipse打开就出错Could not create the Java virtual machine 描述&#xff1a; JVM_terminted_Exit_code1图片&#xff1a;描述&#xff1a; Could_not create_the_Java_virtual_machine图片&#xff1a;myeclipse一启动就自动关闭&#xff0c;报错"Could not creat…

JavaScript高级程序设计 学习笔记3 - 语言基础

内容均摘自JavaScript高级程序设计第四版&#xff0c;仅用于记录学习过程。 虽然这一章都很基础&#xff0c;但是还是有很多小细节需要注意的&#xff01;好好看看吧&#xff5e;进一步细分了书里的目录&#xff0c;这样查询也更方便啦 第三章 语言基础一.语法3.1 语法3.1.1 区…