7.13题目总结

博客围绕二叉树、矩阵和栈展开算法操作。包括将二叉树变换为镜像,顺时针打印矩阵元素,定义含获取最小元素函数的栈结构,判断栈的压入和弹出顺序,以及二叉树的层次遍历等内容,还提及用两个栈实现特定功能。

目录

操作给定的二叉树,将其变换为源二叉树的镜像。

输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下4 X 4矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 则依次打印出数字1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10.

定义栈的数据结构,请在该类型中实现一个能够得到栈中所含最小元素的min函数(时间复杂度应为O(1))。

输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否可能为该栈的弹出顺序。假设压入栈的所有数字均不相等。例如序列1,2,3,4,5是某栈的压入顺序,序列4,5,3,2,1是该压栈序列对应的一个弹出序列,但4,3,5,1,2就不可能是该压栈序列的弹出序列。(注意:这两个序列的长度是相等的)

从上往下打印出二叉树的每个节点,同层节点从左至右打印


操作给定的二叉树,将其变换为源二叉树的镜像。

    //操作给定的二叉树,将其变换为源二叉树的镜像。(使用递归的方法来实现)
    public void Mirror(Node root) {
        Node temp = null;
        if(root == null){
            return;
        }
        if(root.left == null && root.right == null){
            return;
        }

        temp = root.right;
        root.right = root.left;
        root.left = temp;//交换字节点

        if(root.left != null)
        Mirror(root.left);
        if(root.right != null)
        Mirror(root.right);
    }
    public void MirrorNotRec(Node root) {//非递归来实现
        if(root == null){
            return;
        }
        Stack<Node> stack = new Stack<>();
        stack.push(root);
        while(!stack.isEmpty()){///////使用while(stack != null)会提示空栈异常,不能这样使用
            Node node = stack.pop();
            if(node.left != null || node.right != null){
                Node temp = node.left;
                node.left = node.right;
                node.right = temp;
            }
            if(node.left != null){//注意,这里先左后右
                stack.push(node.left);
            }
            if(node.right != null){
                stack.push(node.right);
            }

        }
    }

输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下4 X 4矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 则依次打印出数字1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10.

    public ArrayList<Integer> printMatrix(int[][] matrix) {
        if (matrix.length == 0 || matrix[0].length == 0) {
            return null;
        }
        ArrayList<Integer> arrayList = new ArrayList<>();

        int tR = 0;
        int tC = 0;

        int dR = matrix.length - 1;
        int dC = matrix[0].length - 1;

        while (tR <= dR && tC <= dC) {
            if (tR == dR) {
                for (int i = tC; i <= dC; i++) {
                    arrayList.add(matrix[tR][i]);
                }
            } else if (tC == dC) {
                for (int i = tR; i <= dR; i++) {
                    arrayList.add(matrix[i][tC]);
                }
            } else {
                int curR = tR;
                int curC = tC;
                while (curC != dC) {
                    arrayList.add(matrix[tR][curC]);
                    curC++;
                }
                while (curR != dR) {
                    arrayList.add(matrix[curR][dC]);
                    curR++;
                }
                while (curC != tC) {
                    arrayList.add(matrix[dR][curC]);
                    curC--;
                }
                while (curR != tR) {
                    arrayList.add(matrix[curR][tC]);
                    curR--;
                }
            }
            tR++;
            tC++;
            dR--;
            dC--;
        }
        return arrayList;
    }
或者分开来写:
    public ArrayList<Integer> printMatrix2(int[][] matrix) {
        if (matrix.length == 0 || matrix[0].length == 0) {
            return null;
        }
        ArrayList<Integer> arrayList = new ArrayList<>();

        int tR = 0;
        int tC = 0;

        int dR = matrix.length - 1;
        int dC = matrix[0].length - 1;
        while(tR <= dR && tC <= dC){
            arrayList.addAll(PrintSubMatix(matrix,tR++,tC++,dR--,dC--));
        }
        return arrayList;
    }

    public ArrayList<Integer> PrintSubMatix(int[][] matrix, int tR, int tC, int dR, int dC) {
        ArrayList<Integer> arrayList = new ArrayList<>();

        if (tR == dR) {
            for (int i = tC; i <= dC; i++) {
                arrayList.add(matrix[tR][i]);
            }
        } else if (tC == dC) {
            for (int i = tR; i <= dR; i++) {
                arrayList.add(matrix[i][tC]);
            }
        } else {
            int curR = tR;
            int curC = tC;
            while (curC != dC) {
                arrayList.add(matrix[tR][curC]);
                curC++;
            }
            while (curR != dR) {
                arrayList.add(matrix[curR][dC]);
                curR++;
            }
            while (curC != tC) {
                arrayList.add(matrix[dR][curC]);
                curC--;
            }
            while (curR != tR) {
                arrayList.add(matrix[curR][tC]);
                curR--;
            }
        }
        return arrayList;
    }

定义栈的数据结构,请在该类型中实现一个能够得到栈中所含最小元素的min函数(时间复杂度应为O(1))。

用两个栈来实现:一个栈用来正常的pop等操作,一个栈用来作为getMin的辅助栈, // 每次压入的数为 已辅助栈顶元素 和 新进数 中最小的一个数

public class getminStackTest {
    //定义栈的数据结构,请在该类型中实现一个能够得到栈中所含最小元素的min函数(时间复杂度应为O(1))
    // 用两个栈来实现:一个栈用来正常的pop等操作,一个栈用来作为getMin的辅助栈,
    // 每次压入的数为 已辅助栈顶元素 和 新进数 中最小的一个数
    Stack<Integer> stack1 = new Stack<>();
    Stack<Integer> stack2 = new Stack<>();
    public void push(int node) {
        stack1.push(node);

        if(stack2.isEmpty() || node <= stack2.peek()){
            stack2.push(node);
        }else {
            stack2.push(stack2.peek());
        }
    }

    public void pop() {
            stack1.pop();
            stack2.pop();
    }

    public int top() {
        return stack1.peek();
    }

    public int min() {
            return stack2.peek();
    }
}

输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否可能为该栈的弹出顺序。假设压入栈的所有数字均不相等。例如序列1,2,3,4,5是某栈的压入顺序,序列4,5,3,2,1是该压栈序列对应的一个弹出序列,但4,3,5,1,2就不可能是该压栈序列的弹出序列。(注意:这两个序列的长度是相等的)

将pushA依次压入栈中,按照popA的方式依次弹出

    public boolean IsPopOrder(int [] pushA,int [] popA) {
        //将pushA依次压入栈中,按照popA的方式依次弹出
        if(pushA.length != popA.length || popA.length == 0){
            return false;
        }
        Stack<Integer> stack = new Stack<>();
        int cur = 0;
        for(int i = 0; i <= pushA.length-1; i++){
            stack.push(pushA[i]);//压入元素
            while (!stack.isEmpty() && stack.peek() == popA[cur]){//栈顶元素如果与数组中cur未知的元素相等则弹出
                stack.pop();
                cur++;
                if(stack.isEmpty()){
                    return true;//栈中元素都弹出了,则为真(数组长度相等)
                }
            }
        }
        return false;
    }

从上往下打印出二叉树的每个节点,同层节点从左至右打印

相当于二叉树的层次遍历

    public ArrayList<Integer> PrintFromTopToBottom(Node root) {
        Queue<Node> queue = new LinkedList();
        ArrayList<Integer> array = new ArrayList<>();
        if(root == null){
            return array;
        }
        queue.add(root);
        while(!queue.isEmpty()){
            Node node = queue.poll();
            array.add(node.value);
            System.out.println(node.value);
            if(node.left != null){
                queue.add(node.left);
            }
            if(node.right != null){
                queue.add(node.right);
            }
        }
        return array;
    }
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值