美文网首页
12.2 剑指Offer 17-22

12.2 剑指Offer 17-22

作者: AlexLJS | 来源:发表于2020-08-06 11:51 被阅读0次

17、

public class HasSubtree_17 {
    /**
     * 树的子结构
     * 题目描述
     * 输入两棵二叉树A,B,判断B是不是A的子结构。
     * (ps:我们约定空树不是任意一个树的子结构)
     *
     * 思路:
     *  没什么更好的方法,目前就是遍历二叉树,找到相同的node ,
     *
     *  模式树指针和当前指针一起往下走,judge值是否相同。
     */
    public boolean HasSubtree(TreeNode root1, TreeNode root2) {
        if (root2 == null) return false;
        if (root1 == null) return root2 == null? true:false;
        Stack<TreeNode> stack = new Stack<>();
        stack.push(root1);
        while (!stack.isEmpty()){
            TreeNode temp = stack.pop();
            //do sth
            if (temp.val == root2.val && judge(temp, root2)) return true;
            if (temp.right != null) stack.push(temp.right);
            if (temp.left != null) stack.push(temp.left);
        }
        return false;
    }
    public boolean judge(TreeNode root1, TreeNode root2){
        if (root1 == null) return root2 == null? true:false;
        if (root2 == null) return true; //主树可以不遍历到null

        if (root1.val != root2.val) return false;
        return judge(root1.left, root2.left) && judge(root1.right, root2.right);
    }
}


18、

public class MirrorBinaryTree_18 {
    /**
     * 题目描述
     * 操作给定的二叉树,将其变换为源二叉树的镜像。
     *
     * 思路:
     * 这题很简单递归翻转左右节点
     */
    public void Mirror(TreeNode root) {
        if (root == null) return;
        Mirror(root.left);
        Mirror(root.right);
        TreeNode temp = root.left;
        root.left = root.right;
        root.right = temp;
    }

}

19、

public class PrintMatrix_19 {
    /**
     *
     *顺序打印矩阵
     *题目描述
     *输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,
     * 例如,如果输入如下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) {
        ArrayList<Integer> res = new ArrayList<>();
        process(matrix,0,0,matrix.length-1,matrix[0].length-1,res);
        return res;
    }

    public void process(int[][] matrix, int li, int lj, int ri, int rj, ArrayList<Integer> res){
        if (li > ri || lj > rj) return;
        if (li == ri) while (lj <= rj) res.add(matrix[li][lj++]);
        if (lj == rj) while (li <= ri) res.add(matrix[li++][lj]);

        int i;int j;
        for (i = li,j = lj; j < rj; j++) { res.add(matrix[i][j]); }
        for (i = li,j = rj; i < ri; i++) { res.add(matrix[i][j]); }
        for (i = ri,j = rj; j > lj; j--) { res.add(matrix[i][j]); }
        for (i = ri,j = lj; i > li; i--) { res.add(matrix[i][j]); }

        process(matrix,++li,++lj,--ri,--rj,res);
    }
}

20、

public class MinStack_20 {
    /**
     * 最小值栈
     * 题目描述
     * 定义栈的数据结构,请在该类型中实现一个能够得到栈中所含最小元素的min函数(时间复杂度应为O(1))。
     * 注意:保证测试中不会当栈为空的时候,对栈调用pop()或者min()或者top()方法。
     *
     * 思路:
     * 这个问题核心在于最小值出栈后,如何找到之前的最小值,所以min数据结构一定是集合。
     * 由于时间复杂度的限制是不可以遍历集合,所以考虑用stack。
     */
    private Stack<Integer> data = new Stack<>();
    private Stack<Integer> min = new Stack<>();

    public void push(int node) {
        if (min.isEmpty() || node <= min.peek()) min.push(node);

        data.push(node);
    }

    public void pop() {
        if (data.pop() == min.peek()) min.pop();
    }

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

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

21、

public class IsPopOrder_21 {
    /**
     * 栈的压入弹出
     *
     * 题目描述
     * 输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否可能为该栈的弹出顺序。
     * 假设压入栈的所有数字均不相等。
     * 例如序列1,2,3,4,5是某栈的压入顺序,序列4,5,3,2,1是该压栈序列对应的一个弹出序列,
     * 但4,3,5,1,2就不可能是该压栈序列的弹出序列。(注意:这两个序列的长度是相等的)
     *
     * 思路:
     * 用stack模拟出入栈过程。每push一个值之前,都检验栈顶元素是否该出栈。(等于pop[j])
     * 该出则出,直到所有元素都入栈。
     * 
     * 检验出栈是否pop[]是否出完 , stack pop同时移动。pop空,stack空,即为true。
     */

    public boolean IsPopOrder(int [] pushA,int [] popA) {
        if (pushA.length != popA.length) return false;

        Stack<Integer> stack = new Stack<>();
        int i = 0;
        int j = 0;

        while (i < popA.length) {
            if (stack.isEmpty() || stack.peek() != popA[j]){
                stack.push(pushA[i++]);
            }else {
                j++;
                stack.pop();
            }
        }
        while (j < popA.length && popA[j] == stack.peek()){
            j++;
            stack.pop();
        }

        return stack.isEmpty()? true:false;
    }
}

22、

public class PrintFromTopToBottom_22 {
    /**
     * 从上往下打印二叉树
     * 题目描述
     * 从上往下打印出二叉树的每个节点,同层节点从左至右打印。
     *
     * 思路:
     * 二叉树的层序遍历,可以解决很多二叉树问题的基石。
     * 核心就是 使用 队列queue。
     */

    public ArrayList<Integer> PrintFromTopToBottom(TreeNode root) {
        ArrayList<Integer> res = new ArrayList<>();
        if (root == null) return res;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        while (!queue.isEmpty()){
            TreeNode ele = queue.poll();
            res.add(ele.val);
            if (ele.left != null) queue.add(ele.left);
            if (ele.right != null) queue.add(ele.right);
        }
        return res;
    }
}

相关文章

  • 12.2 剑指Offer 17-22

    17、 18、 19、 20、 21、 22、

  • 全网最全剑指offer题目解答

    【剑指offer】Java版代码(完整版) 【剑指offer】1-10题 【剑指offer】11-20题 【剑指o...

  • 剑指offer 和 leetcode题目

    剑指offer leetcode

  • 单例模式

    单例模式 最近在看《剑指offer》,根据《剑指offer》的讲解,结合《effectiveJava》简单学习了一...

  • 年龄排序

    摘抄资料:《剑指offer》

  • 剑指offer

    第一题:在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,...

  • 《剑指offer》

    4.调整数组顺序使奇数位于偶数前面 题目描述:输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇...

  • 剑指offer

    二维数组中查找数在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到...

  • 剑指offer

    剑指offer题目 1 一只青蛙一次可以跳上1级台阶,也可以跳上2级。求该青蛙跳上一个n级的台阶总共有多少种跳法。...

  • 剑指offer

    1.链表反转 1、输出倒数第k个数

网友评论

      本文标题:12.2 剑指Offer 17-22

      本文链接:https://www.haomeiwen.com/subject/hkblcktx.html