题目1

题目描述

输入两棵二叉树A,B,判断B是不是A的子结构。(ps:我们约定空树不是任意一个树的子结构)
/**
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null; public TreeNode(int val) {
this.val = val; } }
*/
public class Solution {
public boolean HasSubtree(TreeNode root1,TreeNode root2) { boolean result = false; if(root1!=null&&root2!=null){
if(root1.val==root2.val){
result=checkSubTree(root1,root2);
}
if(!result)
result = HasSubtree(root1.left,root2);
if(!result)
result = HasSubtree(root1.right,root2);
} return result;
} public boolean checkSubTree(TreeNode root1,TreeNode root2) {
if(root2==null)
return true;
if(root1==null)
return false;
if(root1.val!=root2.val)
return false;
return checkSubTree(root1.left,root2.left)&&checkSubTree(root1.right,root2.right); }
}

题目2

题目描述(二叉树的反转)

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

输入描述:

二叉树的镜像定义:源二叉树
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 tmp = root.left;
root.left = root.right;
root.right = tmp; Mirror(root.left);
Mirror(root.right);
}
}

题目3

题目描述

输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下矩阵: 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.

这个做了有点久,第一次做的如下(分四种主要情况):

import java.util.ArrayList;
public class Solution {
ArrayList<Integer> list = new ArrayList<Integer>();
public ArrayList<Integer> printMatrix(int [][] matrix) {
if(matrix==null)
return list;
int a=0,b=0,a_length = matrix.length,b_length = matrix[0].length; while(a_length>=1&&b_length>=1){
sysoutArray(matrix,a,b,a_length,b_length);
a++;
b++;
a_length-=2;
b_length-=2;
}
return list;
} public void sysoutArray(int [][] matrix,int a,int b,int a_length,int b_length){
//单个的情况
if(a_length==b_length&&a_length==1){
list.add(matrix[a][b]);
return;
}
//两种单行的情况
if(a_length==1){
for(int i=b;i<b+b_length;i++)
list.add(matrix[a][i]);
return;
}
if(b_length==1){
for(int i=0;i<a+a_length;i++)
list.add(matrix[i][b]);
return;
}
//可以顺时针旋转打印的情况
for(int i=b;i<b+b_length-1;i++)
list.add(matrix[a][i]);
for(int i=a;i<a+a_length-1;i++)
list.add(matrix[i][b+b_length-1]);
for(int i=b+b_length-1;i>=a+1;i--)
list.add(matrix[a+a_length-1][i]);
for(int i=a+a_length-1;i>=a+1;i--)
list.add(matrix[i][b]);
} }

题目4

题目描述

定义栈的数据结构,请在该类型中实现一个能够得到栈最小元素的min函数。

import java.util.Stack;

public class Solution {

    private Stack<Integer> stack = new Stack<Integer>();
private Stack<Integer> minStack = new Stack<Integer>();
private Integer minFlag=null; public void push(int node) {
if(minFlag!=null){
int min = minStack.peek();
if(node<min){
minFlag=node;
minStack.push(node);
}else{
minFlag=min;
minStack.push(min);
}
}else{
minFlag=node;
minStack.push(node);
}
stack.push(node);
} public void pop() {
stack.pop();
minStack.pop();
} public int top() {
return stack.peek();
} public int min() {
return minStack.peek();
}
}

题目5

题目描述

输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否为该栈的弹出顺序。假设压入栈的所有数字均不相等。例如序列1,2,3,4,5是某栈的压入顺序,序列4,5,3,2,1是该压栈序列对应的一个弹出序列,但4,3,5,1,2就不可能是该压栈序列的弹出序列。(注意:这两个序列的长度是相等的)
import java.util.ArrayList;
import java.util.Stack; public class Solution {
public boolean IsPopOrder(int [] pushA,int [] popA) {
if(pushA==null||popA==null||pushA.length==0||popA.length==0)
return false;
Stack<Integer> stack = new Stack<Integer>();
int index=0;
for(int i=0;i<pushA.length;i++){
stack.push(pushA[i]);
while(!stack.empty()&&stack.peek()==popA[index]){
stack.pop();
index++;
}
}
return stack.empty();
}
}

题目6

题目描述

从上往下打印出二叉树的每个节点,同层节点从左至右打印。(二叉树的层序遍历)
import java.util.*;
/**
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null; public TreeNode(int val) {
this.val = val; } }
*/
public class Solution {
public ArrayList<Integer> PrintFromTopToBottom(TreeNode root) {
ArrayList<Integer> list = new ArrayList<Integer>(); if(root==null)
return list; Queue<TreeNode> queue = new LinkedList<TreeNode>();
queue.offer(root); while(!queue.isEmpty()){ TreeNode treeNode = queue.poll(); if(treeNode.left!=null){
queue.offer(treeNode.left);
}
if(treeNode.right!=null){
queue.offer(treeNode.right);
}
list.add(treeNode.val);
} return list;
}
}

算法学习之剑指offer(四)的更多相关文章

  1. 算法学习之剑指offer(十一)

    一 题目描述 请实现一个函数按照之字形打印二叉树,即第一行按照从左到右的顺序打印,第二层按照从右至左的顺序打印,第三行按照从左到右的顺序打印,其他行以此类推. import java.util.*; ...

  2. 算法学习之剑指offer(九)

    一 题目描述 求1+2+3+...+n,要求不能使用乘除法.for.while.if.else.switch.case等关键字及条件判断语句(A?B:C). public class Solution ...

  3. 算法学习之剑指offer(十二)

    一 题目描述 请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径.路径可以从矩阵中的任意一个格子开始,每一步可以在矩阵中向左,向右,向上,向下移动一个格子.如果一条路径经过了矩 ...

  4. 算法学习之剑指offer(十)

    一 题目描述 请实现一个函数用来判断字符串是否表示数值(包括整数和小数).例如,字符串"+100","5e2","-123","3 ...

  5. 算法学习之剑指offer(八)

    题目一 题目描述 小明很喜欢数学,有一天他在做数学作业时,要求计算出9~16的和,他马上就写出了正确答案是100.但是他并不满足于此,他在想究竟有多少种连续的正数序列的和为100(至少包括两个数).没 ...

  6. 算法学习之剑指offer(六)

    题目1 题目描述 输入n个整数,找出其中最小的K个数.例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4,. import java.util.*; public cl ...

  7. 算法学习之剑指offer(五)

    题目1 题目描述 输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果.如果是则输出Yes,否则输出No.假设输入的数组的任意两个数字都互不相同. public class Solution ...

  8. 算法学习之剑指offer(一)

    题目一: 题目描述 在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数. 思路1:遍历 ...

  9. 算法学习之剑指offer(七)

    题目1 题目描述 在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对.输入一个数组,求出这个数组中的逆序对的总数P.并将P对1000000007取模的结果输出. 即输出P% ...

随机推荐

  1. Elasticsearch之更新

    public class UpdateElasticAPI { private static RestClient restClient; static { restClient=RestClient ...

  2. 关于git远程被覆盖的问题

    有同事A和B,git远程版本为A0,两个人的本地项目已经跟远程同步.同事A先向git提交了3次,A1.A2.A3.git远程版本为A0.A1.A2.A3.同事B也向git提交了1次B1,但是同事B提交 ...

  3. Spring Boot (二):模版引擎 Thymeleaf 渲染 Web 页面

    Spring Boot (二):模版引擎 Thymeleaf 渲染 Web 页面 在<Spring Boot(一):快速开始>中介绍了如何使用 Spring Boot 构建一个工程,并且提 ...

  4. Winform中自定义xml配置文件后对节点进行读取与写入

    场景 Winform中自定义xml配置文件,并配置获取文件路径: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/100522648 ...

  5. NOIP2008复赛 提高组 第一题

    描述 笨小猴的词汇量很小,所以每次做英语选择题的时候都很头疼.但是他找到了一种方法,经试验证明,用这种方法去选择选项的时候选对的几率非常大! 这种方法的具体描述如下:假设maxn是单词中出现次数最多的 ...

  6. ZK Watcher 的原理和实现

    什么是 ZK Watcher 基于 ZK 的应用程序的一个常见需求是需要知道 ZK 集合的状态.为了达到这个目的,一种方法是 ZK 客户端定时轮询 ZK 集合,检查系统状态是否发生了变化.然而,轮询并 ...

  7. mysql ER图

        ER 图 ER图也被称为实体-联系图,提供了表示实体类型.属性和联系的方法,下图就是典型的一张ER图. ER图主要由四个成分构成: 1 实体 实体是客观世界中存在的各种事物,或者某个抽象事件, ...

  8. 初识Node.js之Node与java作为后台服务器的对比

    > 文章原创于公众号:程序猿周先森.本平台不定时更新,喜欢我的文章,欢迎关注我的微信公众号. ![file](https://img2018.cnblogs.com/blog/830272/20 ...

  9. linux文件系统分区、格式化、挂载、卷标挂载、永久挂载

    思想不放松你的行为就不会放松,你的行为放松了,说明你的思想放松了.

  10. BCD 码、Gray 码、ASCII 码都是什么呢?

    BCD 码:即(Binary Coded Decimal)码,也称为 8421 码,是十进制代码中最常见的一种.每一位的 1 代表的十进制数称为这一位的权.BCD 码中每一位的权都是固定不变的,它属于 ...