zoukankan      html  css  js  c++  java
  • 递归相关题目

    二叉树的镜像

    题目描述

    操作给定的二叉树,将其变换为源二叉树的镜像。 
    输入描述:
    二叉树的镜像定义:源二叉树 
        	    8
        	   /  
        	  6   10
        	 /   / 
        	5  7 9 11
        	镜像二叉树
        	    8
        	   /  
        	  10   6
        	 /   / 
        	11 9 7  5
    /**
    public class TreeNode {
        int val = 0;
        TreeNode left = null;
        TreeNode right = null;
    
        public TreeNode(int val) {
            this.val = val;
    
        }
    
    }
    */ 
    public class Solution {
        public void Mirror(TreeNode root) {
            if(root == null){
                return;
            }
            TreeNode subLeft = root.left;
            TreeNode subRight = root.right;
            root.left = subRight;
            root.right = subLeft;
            if(subLeft != null)
                Mirror(subLeft);
            if(subRight != null)
                Mirror(subRight);
            if(root.left == null || root.right == null){
                return;
            }      
           
        }
    }
    顺时针打印矩阵
    • 参与人数:865时间限制:1秒空间限制:32768K
    • 通过比例:16.93%
    • 最佳记录:0 ms|8552K(来自  handsomexian

    题目描述

    输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下矩阵: 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.
     思路:先得到矩阵的行数和列数
    编写打印函数print(), 类似Exce厘面,由两点确定一个数据域,给定左上顶点和右下顶点;
    print()函数打印原理如下图所示.
    打印时重点要注意:最后剩下的区域可能出现4种情况
    1, 为空  4*4矩阵
    2, 一点  3*3矩阵
    3, 一行  3*4矩阵
    4,一列   4*3矩阵
    2,3,4情况需要单独考虑
     
    import java.util.ArrayList;
    
    public class Solution {
        ArrayList<Integer> result = new ArrayList<Integer>();
    
        public ArrayList<Integer> printMatrix(int[][] matrix) {
            int row = matrix.length;
            int column = matrix[0].length;
            if (row == 0) {
                return null;
            }
    
            int startx = 0;
            int starty = 0;
            int endx = column - 1;
            int endy = row - 1;
            while (startx < endx && starty < endy) {
                print(startx, starty, endx, endy, matrix);
    
                startx++;
                starty++;
                endx--;
                endy--;
            }
            // 一列
            if (startx == endx && starty < endy) {
                for (int i = starty; i <= endy; i++) {
                    result.add(matrix[i][startx]);
                }
    
            }
            // 一行
            if (startx < endx && starty == endy) {
                for (int i = startx; i <= endx; i++) {
                    result.add(matrix[starty][i]);
                }
            }
            // 一点
            if (startx == endx && starty == endy)
                result.add(matrix[startx][starty]);
            return result;
        }
    
        public void print(int startx, int starty, int endx, int endy, int[][] matrix) {
            int e1 = starty;
            for (int i = startx; i < endx; i++) {
                result.add(matrix[e1][i]);
            }
    
            int e2 = endx;
            for (int i = starty; i < endy; i++) {
                result.add(matrix[i][e2]);
            }
    
            int e3 = endy;
            for (int i = endx; i > startx; i--) {
                result.add(matrix[e3][i]);
            }
    
            int e4 = startx;
            for (int i = endy; i > starty; i--) {
                result.add(matrix[i][e4]);
            }
    
        }
    
    }
    包含min函数的栈
    • 参与人数:972时间限制:1秒空间限制:32768K
    • 通过比例:34.18%
    • 最佳记录:0 ms|8552K(来自  handsomexian

    题目描述

    定义栈的数据结构,请在该类型中实现一个能够得到栈最小元素的min函数。
     
    错误想法:每次压入一个新元素进栈时,将栈里的所有元素排序,让最小的元素位于栈顶
    但这种思路不能保证最后压人的元素能够最先出栈,因此这个数据结构已经不是栈啦.
     
    因此要考虑将最小元素单独存起来,但最小元素出栈后,如何解决下一个最小元素的问题,
    显然存放最小元素的也应该是一个类似栈的结构,哈哈   辅助栈
    import java.util.Stack;
    
    public class Solution {
    
        Stack<Integer> min  = new Stack<Integer>();
        Stack<Integer> data  = new Stack<Integer>();
        
        public void push(int node) {
            data.push(node);
            if(min.empty()){
                min.push(node);
            }else{
                int temp = min.peek();
                if(node <= temp){
                    min.push(node);
                }
            }
        }
        
        public void pop() {
            int temp = data.pop();
            if(temp == min.peek()){
                min.pop();    
            }
        }
        
        public int top() {
            return data.peek();
        }
        
        public int min() {
            return min.peek();
        }
    }
    栈的压入、弹出序列
    • 参与人数:932时间限制:1秒空间限制:32768K
    • 通过比例:24.89%
    • 最佳记录:0 ms|8552K(来自  最後/tx武壵

    题目描述

    输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否为该栈的弹出顺序。假设压入栈的所有数字均不相等。例如序列1,2,3,4,5是某栈的压入顺序,序列4,5,3,2,1是该压栈序列对应的一个弹出序列,但4,3,5,1,2就不可能是该压栈序列的弹出序列。
     
     
    import java.util.ArrayList;
    
    public class Solution {
        public boolean IsPopOrder(int [] pushA,int [] popA) {
            
            ArrayList<Integer> used = new ArrayList<Integer>();
            int len = pushA.length;
            if(popA.length != len || len==0){
                return false;
            }
            int i=0,j;
            for(j=0; j<len; j++){
                int temp = popA[j];                  
                while(i<len){
                    if(pushA[i] == temp){
                        used.add(pushA[i]);
                        i++;
                        break;
                    }
                    used.add(pushA[i]);
                    i++;        
                }
                
                int index = used.size()-1;
                if(temp == used.get(index)){
                    used.remove(index);    
                }else{
                    return false;
                }
       
            }
            
            return true;
        }
    }
    从上往下打印二叉树
    • 参与人数:1067时间限制:1秒空间限制:32768K
    • 通过比例:26.95%
    • 最佳记录:0 ms|8552K(来自  疯子~a

    题目描述

    从上往下打印出二叉树的每个节点,同层节点从左至右打印。
    特别要注意:
      if(root == null){
                return res;
       }

    不可以换成 return null 因为ArrayList为空时,因为重写了toString(),所以仍然可以打印出两端括号,和null并不相同
    import java.util.ArrayList;
    /**
    public class TreeNode {
        int val = 0;
        TreeNode left = null;
        TreeNode right = null;
    
        public TreeNode(int val) {
            this.val = val;
    
        }
    
    }
    */
    public class Solution { 
        ArrayList<Integer>  res = new ArrayList<Integer>();
        ArrayList<TreeNode>  queue = new ArrayList<TreeNode>();
        public ArrayList<Integer> PrintFromTopToBottom(TreeNode root) {
            if(root == null){
                return res;
            }
            queue.add(root);
            while (queue.size() > 0){
                TreeNode temp = queue.get(0);
                queue.remove(0);
                res.add(temp.val);
                
                if(temp.left != null){
                    queue.add(temp.left);
                }
                
                if(temp.right != null){
                    queue.add(temp.right);
                }
            }
            
            return res;
            
        }
    }
  • 相关阅读:
    PAT 甲级 1132 Cut Integer (20 分)
    AcWing 7.混合背包问题
    AcWing 9. 分组背包问题
    AcWing 5. 多重背包问题 II
    AcWing 3. 完全背包问题
    AcWing 4. 多重背包问题
    AcWing 2. 01背包问题
    AcWing 875. 快速幂
    AcWing 874. 筛法求欧拉函数
    AcWing 873. 欧拉函数
  • 原文地址:https://www.cnblogs.com/hixin/p/4768056.html
Copyright © 2011-2022 走看看