题目描述

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

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;
} }

最新文章

  1. Atitit.日志系统slf4j的使用
  2. C# Async与Await的使用
  3. 转:关于垂直网格与CSS基线对其的探讨
  4. IOS插件管理器: alcatraz
  5. JS中的this用法详解
  6. 区分jquery中的offset和position
  7. 通知栏快捷按钮自定义教程以及快捷面板提取的思路-转自魔趣论坛-lonyii2
  8. Unity 移动MM自签名方式
  9. 查看MDB格式文件数据表
  10. [置顶] boost使用(六)
  11. ASP.NET MVC5 学习笔记-5 测试
  12. commview for wifi 破解无线
  13. JDBC(MySQL)一周学习总结(二)
  14. SpringMVC---CookieValue
  15. (四):C++分布式框架——状态中心模块
  16. mysql 主从同步 mysql代理服务器
  17. 从零开始学习PYTHON3讲义(三)写第一个程序
  18. [转]js 取得 Unix时间戳(Unix timestamp)
  19. JAVA8给我带了什么——并行流和接口新功能
  20. js的以及前端框架

热门文章

  1. ORACLE SQL语句练习题
  2. Yum未完成事务问题
  3. hadoop 通过distcp进行并行复制
  4. 树莓派4B安装docker-compose(64位Linux)
  5. 一套基于SpringBoot+Vue+Shiro 前后端分离 开发的代码生成器
  6. .NetCore 网站DELETE请求错误405.0 - Method Not Allowed 因为使用了无效方法
  7. [Leetcode] 第289题 生命游戏
  8. 前端面试题——html与css基础篇
  9. 转载:alpha测试和beta测试的区别;黑盒测试和白盒测试的区别;
  10. [经验栈]SQL语句逻辑运算符&quot;AND&quot;、&quot;&amp;&amp;&quot;兼容性