题目描述

请实现一个函数按照之字形打印二叉树,即第一行按照从左到右的顺序打印,第二层按照从右至左的顺序打印,第三行按照从左到右的顺序打印,其他行以此类推。

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<ArrayList<Integer> > Print(TreeNode pRoot) {
ArrayList<ArrayList<Integer> > list = new ArrayList<ArrayList<Integer> >();
int layer=1;
Stack<TreeNode> stack1 = new Stack<>();
stack1.push(pRoot);
Stack<TreeNode> stack2 = new Stack<>(); while(!stack1.empty()||!stack2.empty()){
if(layer%2==1){
ArrayList<Integer> tmp = new ArrayList<Integer>();
while(!stack1.empty()){
TreeNode node = stack1.pop();
if(node!=null){
tmp.add(node.val);
stack2.push(node.left);
stack2.push(node.right);
}
}
if(!tmp.isEmpty())
list.add(tmp);
layer++;
}else{
ArrayList<Integer> tmp = new ArrayList<Integer>();
while(!stack2.empty()){
TreeNode node = stack2.pop();
if(node!=null){
tmp.add(node.val);
stack1.push(node.right);
stack1.push(node.left);
}
}
if(!tmp.isEmpty())
list.add(tmp);
layer++;
}
} return list; } }

题目描述

从上到下按层打印二叉树,同一层结点从左至右输出。每一层输出一行。
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 {
ArrayList<ArrayList<Integer> > Print(TreeNode pRoot) {
ArrayList<ArrayList<Integer> > list = new ArrayList<ArrayList<Integer> >();
ArrayList<TreeNode> tmpList = new ArrayList<TreeNode>();
if(pRoot==null)
return list;
tmpList.add(pRoot); while(!tmpList.isEmpty()){
ArrayList<TreeNode> tmpList2 = new ArrayList<TreeNode>();
ArrayList<Integer> tmpInt = new ArrayList<Integer>();
for(int i=0;i<tmpList.size();i++){
tmpInt.add(tmpList.get(i).val);
if(tmpList.get(i).left!=null)
tmpList2.add(tmpList.get(i).left);
if(tmpList.get(i).right!=null)
tmpList2.add(tmpList.get(i).right);
}
list.add(tmpInt);
tmpList = new ArrayList<TreeNode>(tmpList2);
}
return list;
} }

题目描述

请实现两个函数,分别用来序列化和反序列化二叉树

/*
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null; public TreeNode(int val) {
this.val = val; } }
*/
public class Solution {
String Serialize(TreeNode root) {
StringBuffer sb = new StringBuffer();
if(root==null)
{
sb.append("#,");
return sb.toString();
}
sb.append(root.val+",");
sb.append(Serialize(root.left));
sb.append(Serialize(root.right));
return sb.toString();
}
private int index=-1;
TreeNode Deserialize(String str) {
index++;
int len = str.length();
if(index >= len){
return null;
}
String[] strr = str.split(",");
TreeNode node = null;
if(!strr[index].equals("#")){
node = new TreeNode(Integer.valueOf(strr[index]));
node.left = Deserialize(str);
node.right = Deserialize(str);
} return node;
}
}

题目描述

给定一颗二叉搜索树,请找出其中的第k大的结点。例如, 5 / \ 3 7 /\ /\ 2 4 6 8 中,按结点数值大小顺序第三个结点的值为4。

/*
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null; public TreeNode(int val) {
this.val = val; } }
*/
public class Solution {
private int index=0;
TreeNode resultNode; TreeNode KthNode(TreeNode pRoot, int k)
{
if(pRoot == null || k <= 0){
return null;
}
KthNode2(pRoot,k);
return resultNode;
} void KthNode2(TreeNode pRoot, int k){
if(pRoot == null){
return;
}
KthNode(pRoot.left,k);
index++;
if(index == k){
resultNode = pRoot;
}
KthNode(pRoot.right,k);
} }

题目描述

如何得到一个数据流中的中位数?如果从数据流中读出奇数个数值,那么中位数就是所有数值排序之后位于中间的数值。如果从数据流中读出偶数个数值,那么中位数就是所有数值排序之后中间两个数的平均值。

import java.util.*;
public class Solution { private int count=0;
//PriorityQueue默认按自然排序,优先取最小的
private PriorityQueue<Integer> minHeap = new PriorityQueue<>();
private PriorityQueue<Integer> maxHeap = new PriorityQueue<Integer>(new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return o2 - o1;
}
});
//第偶数个插入minHeap,第奇数个插入maxHeap。maxHeap里的数据都小于minHeap里的
public void Insert(Integer num) {
//因为要保证minHeap里的都比maxHeap大,所以先放入maxHeap,再挑maxHeap里最大的放入minHeap
if (count %2 == 0) {
maxHeap.offer(num);
int filteredMaxNum = maxHeap.poll();
minHeap.offer(filteredMaxNum);
} else {
minHeap.offer(num);
int filteredMinNum = minHeap.poll();
maxHeap.offer(filteredMinNum);
}
count++;
} public Double GetMedian() {
if (count %2 == 0) {//偶数取中位数得除2
return new Double((minHeap.peek() + maxHeap.peek())) / 2;
} else {//奇数个,说明最后一个插到minHeap里了
return new Double(minHeap.peek());
}
} }

题目描述

给定一个数组和滑动窗口的大小,找出所有滑动窗口里数值的最大值。例如,如果输入数组{2,3,4,2,6,2,5,1}及滑动窗口的大小3,那么一共存在6个滑动窗口,他们的最大值分别为{4,4,6,6,6,5}; 针对数组{2,3,4,2,6,2,5,1}的滑动窗口有以下6个: {[2,3,4],2,6,2,5,1}, {2,[3,4,2],6,2,5,1}, {2,3,[4,2,6],2,5,1}, {2,3,4,[2,6,2],5,1}, {2,3,4,2,[6,2,5],1}, {2,3,4,2,6,[2,5,1]}。

import java.util.*;
public class Solution { public ArrayList<Integer> maxInWindows(int [] num, int size)
{
ArrayList<Integer> list = new ArrayList<Integer>();
if(num==null||num.length==0||size<=0)
return list;
int i=0,length=num.length;
while((i+size)<=length){
list.add(maxInNums(num,i,i+size-1));
i++;
}
return list;
} public int maxInNums(int [] num, int start,int end)
{
int maxnum=num[start];
for(int i=start+1;i<=end;i++){
if(maxnum<num[i])
maxnum=num[i];
}
return maxnum;
} }

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

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

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

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

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

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

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

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

    题目1 题目描述 输入两棵二叉树A,B,判断B是不是A的子结构.(ps:我们约定空树不是任意一个树的子结构) /** public class TreeNode { int val = 0; Tree ...

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

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

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

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

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

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

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

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

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

    题目1 题目描述 输入一个整数,输出该数二进制表示中1的个数.其中负数用补码表示. 如果一个整数不为0,那么这个整数至少有一位是1.如果我们把这个整数减1,那么原来处在整数最右边的1就会变为0,原来在 ...

随机推荐

  1. .NET Core 3.0 Preview 9 发布

    翻译自官方博客 今天,我们宣布推出.NET Core 3.0 Preview 9.就像预览版8一样,我们专注打磨最终版本的.NET Core 3.0,而不是添加新功能.如果这些最终版本看起来不像早期预 ...

  2. FreeSql 新手上路系列教程已发布在 cnblogs

    FreeSql 是一个功能强大的对象关系映射程序(O/RM),支持 .NETCore 2.1+ 或 .NETFramework 4.5+(QQ群:4336577) FreeSql采用MIT开源协议托管 ...

  3. @Qualifier高级应用---按类别批量依赖注入【享学Spring】

    每篇一句 罗斯:选秀状元可能有水货,但MVP绝对没有 前言 在上篇文章(讲解@LoadBalanced负载均衡)的末尾,我抛出了一个很重要的问题,建议小伙伴自己深入思考一番:本文主要针对此问题,作出一 ...

  4. .Net基础篇_学习笔记_第六天_for循环的几个练习

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  5. Linux 笔记 - 第十三章 Linux 系统日常管理之(三)Linux 系统日志和服务

    博客地址:http://www.moonxy.com 一.前言 日志文件记录了系统每天发生的各种各样的事情,比如监测系统状况.排查问题等.作为系统运维人员可以通过日志来检查错误发生的原因,或者受到攻击 ...

  6. Linux 笔记 - 前三章 CentOS 简介、安装和远程连接

    博客地址:http://www.moonxy.com 一.Unix 和 Linux 的区别 目前主流的 Unix 系统有三种,分别是 IBM-AIX.HP-UX.SUN-Solaris,它们分别运行在 ...

  7. CentOS 7安装后的一些工作记录

    安装net-tools: yum install -y net-tools 安装epel源 yum install epel-release 安装fail2ban yum install fail2b ...

  8. 安装MariaDB

    1.安装MariaDB安装命令yum -y install mariadb mariadb-server安装完成MariaDB,首先启动MariaDBsystemctl start mariadb设置 ...

  9. uni-app开发微信小程序的几天时间

    人只有在不断的学习,才能不断的给自己充电,如果我们停止了学习,就像人没有了血脉,就会死亡,近来学习比较忙,压力比较大,整天面对着电脑,敲击代码,从中虽然收获了快乐,但是换来的确实身体的伤痛,最近虽然自 ...

  10. Kubernetes学习之k8s

    k8s是什么 云原生 越来越多的开发者不仅使用容器作为应用部署和运行的载体,还积极使用了与容器这个应用载体天生匹配的微服务的架构,并依靠容器调度编排引擎的帮助,以保持对外部的敏捷性,这种容器化的微服务 ...