题目描述

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

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. Invalid bound statement (not found): com.taotao.mapper.TbItemMapper.selectByExample问题解决

    最近在做一个关于ssm框架整合的项目,但是今天正合完后出现了问题: Invalid bound statement (not found): com.taotao.mapper.TbItemMappe ...

  2. 使用VUE实现在table中文字信息超过5个隐藏,鼠标移到时弹窗显示全部

    使用VUE实现在table中文字信息超过5个隐藏,鼠标移到时弹窗显示全部 <template> <div> <table> <tr v-for="i ...

  3. C++ 深入浅出工厂模式(初识篇)

    初识工厂模式 我们先看工厂模式的介绍 这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式. 在工厂模式中,我们在创建对象时不会对客户端暴露创建逻辑,并且是通过使用一个共同的接口来指向新创 ...

  4. Slickflow.NET 开源工作流引擎快速入门之三: 简单或分支流程代码编写示例

    前言:对于急切想了解引擎功能的开发人员,在下载版本后,就想尝试编写代码,完成一个流程的开发和测试.本文试图从请假流程,或分支模式来快速了解引擎代码的编写. 1. 创建或分支流程图形 或分支流程是常见的 ...

  5. windows平台:查看端口占用情况,请杀死端口对应进程PID

    //查看 netstat -ano | findstr //杀死 taskkill /f /PID

  6. python SSTI利用

    原理python的SSTI不仅可以向网页插入一些XSS代码,而且还可以获取一些变量和函数信息,尤其是secret_key,如果获取到则可以对flask框架的session可以进行伪造.对于tornad ...

  7. Salesforce学习之路-admin篇

    Salesforce是一款非常强大的CRM(Customer Relationship Management)系统,国外企业使用十分频繁,而国内目前仅有几家在使用(当然,国内外企使用的依旧较多),因此 ...

  8. 表达式树练习实践:C# 循环与循环控制

    目录 表达式树练习实践:C# 循环 LabelTarget for / while 循环 无限循环 最简单的循环 多次循环 break 和 continue 一起 表达式树练习实践:C# 循环 C# ...

  9. Day 24 定时任务

    1.什么是crond crond 就是计划任务,类似于我们平时生活中的闹钟,定点执行. 2.计划任务时间管理 1.Crontab配置文件记录了时间周期的含义 vim /etc/crontab * 表示 ...

  10. 想研究BERT模型?先看看这篇文章吧!

    最近,笔者想研究BERT模型,然而发现想弄懂BERT模型,还得先了解Transformer. 本文尽量贴合Transformer的原论文,但考虑到要易于理解,所以并非逐句翻译,而是根据笔者的个人理解进 ...