【剑指offer】面试题32:从上到下打印二叉树(java)
从上往下打印二叉树的每个节点,同一层的节点按照从左到右的顺序打印。例如输入下图的二叉树,则一次打印出8,6,10,5,7,9,11。
思路:利用队列,将左右子树加入队列末尾,取出结点
代码:
package offer;
import java.util.LinkedList;
import java.util.Queue;
class BineryTree
{
int val;
BineryTree left = null;
BineryTree right = null;
BineryTree(int val)
{
this.val = val;
}
}
public class ti32 {
static void BineryTreeToQueue(BineryTree head)
{
if(head==null)
{
return;
}
Queue<BineryTree> queue = new LinkedList<BineryTree>();
queue.add(head);
while(!queue.isEmpty())
{
BineryTree x = queue.poll();
System.out.println(x.val);
if(x.left!=null)
{
queue.add(x.left);
}
if(x.right!=null)
{
queue.add(x.right);
}
}
}
public static void main(String[] args)
{
BineryTree a = new BineryTree(8);
BineryTree b = new BineryTree(6);
BineryTree c = new BineryTree(10);
BineryTree d = new BineryTree(5);
BineryTree e = new BineryTree(7);
BineryTree f = new BineryTree(9);
BineryTree g = new BineryTree(11);
a.left = b;
a.right = c;
b.left = d;
b.right = e;
c.left = f;
c.right = g;
BineryTreeToQueue(a);
}
}
题目三
请实现一个函数按照之字形打印二叉树,即第一行按照从左到右的顺序打印,第二层按照从右至左的顺序打印,第三行按照从左到右的顺序打印,其他行以此类推。
代码:
package offer;
import java.util.ArrayList;
import java.util.Stack;
class BineryTree
{
int val;
BineryTree left = null;
BineryTree right = null;
BineryTree(int val)
{
this.val = val;
}
}
public class ti32 {
static ArrayList<ArrayList<Integer>> BineryTreeToQueue(BineryTree head)
{
ArrayList<ArrayList<Integer>> alist = new ArrayList<>();
if(head==null)
{
return alist;
}
Stack<BineryTree> stack1 = new Stack<BineryTree>();
Stack<BineryTree> stack2 = new Stack<BineryTree>();
stack1.add(head);
while(!stack1.empty()||!stack2.empty())
{
ArrayList<Integer> list = new ArrayList<>();
if(!stack1.empty())
{
while(!stack1.empty())
{
BineryTree x = stack1.pop();
list.add(x.val);
if(x.left!=null)
{
stack2.add(x.left);
}
if(x.right!=null)
{
stack2.add(x.right);
}
}
}
else
{
while(!stack2.empty())
{
BineryTree x = stack2.pop();
list.add(x.val);
if(x.right!=null)
{
stack1.add(x.right);
}
if(x.left!=null)
{
stack1.add(x.left);
}
}
}
alist.add(list);
}
return alist;
}
public static void main(String[] args)
{
BineryTree a = new BineryTree(8);
BineryTree b = new BineryTree(6);
BineryTree c = new BineryTree(10);
BineryTree d = new BineryTree(5);
BineryTree e = new BineryTree(7);
BineryTree f = new BineryTree(9);
BineryTree g = new BineryTree(11);
a.left = b;
a.right = c;
b.left = d;
b.right = e;
c.left = f;
c.right = g;
ArrayList<ArrayList<Integer>> alist = BineryTreeToQueue(a);
for(int i=0;i<alist.size();i++)
{
for(int j=0;j<alist.get(i).size();j++)
{
System.out.print(alist.get(i).get(j)+" ");
}
System.out.println();
}
}
}
【剑指offer】面试题32:从上到下打印二叉树(java)相关推荐
- 剑指offer面试题[23]-从上往下打印二叉树(按层序打印)
题目描述 从上往下打印出二叉树的每个节点,同层节点从左至右打印. /* struct TreeNode {int val;struct TreeNode *left;struct TreeNode * ...
- 剑指offer(21)从上往下打印二叉树
public class Solution {public ArrayList<Integer> PrintFromTopToBottom(TreeNode root) {//创建俩个数列 ...
- 【剑指offer-Java版】23从上往下打印二叉树
从上往下打印二叉树:如果把这个书看做是一个特殊的图,那么该打印过程就类似于一个广度优先遍历 public class _Q23 {public <T> void PrintTreeFrom ...
- 剑指offer——面试题32:从1到n整数中1出现的次数
剑指offer--面试题32:从1到n整数中1出现的次数 Solution1: 最容易想到的方法: class Solution { public:int NumberOf1Between1AndN_ ...
- 剑指offer——面试题5:从尾到头打印链表
剑指offer--面试题5:从尾到头打印链表 Solution1:我的答案 /** * struct ListNode { * int val; * struct ListNode *next; * ...
- 剑指 Offer 32 . 从上到下打印二叉树
main函数测试代码: 按标准输入输出,比如输入: 3,9,20,null,null,15,7 public static void main(String[] args) {//输入3,9,20,n ...
- 剑指offer 32. 从上到下打印二叉树
声明:本系列博客是对何海涛<剑指offer>的关键点总结. 1.不分行从上到下打印二叉树 1.1. 问题描述 从上到下打印出二叉树的每一个结点,同一层的结点按照从左到右的顺序打印. 如二叉 ...
- 剑指offer——32.从上到下打印二叉树
题目: 从上往下打印出二叉树的每个节点,同层节点从左至右打印. 知识点: 像这种不是按照指针顺序打印的都需要引入辅助空间,由于分析可知,先进先出,因此我们引入了两端都可进出的队列deque,常用操作, ...
- [剑指offer]面试题第[68-2]题[Leetcode][第236题][JAVA][二叉搜索树的最近公共祖先][递归]
[问题描述][中等] 235/68-1 搜索二叉树 236/68-2 二叉树 [解答思路] 递归 时间复杂度:O(N) 空间复杂度:O(N) 情况 1. , 2. , 3. , 4. 的展开写法如下. ...
最新文章
- 个人在 laravel 开发中使用到的一些技巧(持续更新)
- 计算机一级题资源,计算机一级B考题汇总(珍贵资源)
- 在C#中调用windows API函数
- Linux设备驱动程序学习(4) -高级字符驱动程序操作[(1)ioctl and llseek]
- autocad完全应用指南_如何提高CAD画图的速度?有哪些途径和技法?【AutoCAD教程】...
- 二逼平衡树 题解(树套树)
- xlwings 安装及排错: DLL load failed while importing win32api
- 利用python合并csv文件
- 李想的理想,不太「理想」
- Java+MySQL实现网络爬虫程序
- 688. 骑士在棋盘上的概率(中等 动态规划)
- 凌动z3735f运行64位linux,iwork8平板电脑安装ubuntu,Z3735d/f系列CPU通用
- 求当前高度=n时,值x=多少?求解题思路
- 使用七牛云存储解决app部署问题,免申请https认证
- IPFS何时落地应用?FIL价值破千?
- git pack文件过大
- 在线协作编辑器EtherPad使用说明
- 自定义控件其实很简单 五
- jQuery - 设置div的内容和属性
- 【BZOJ5405】platform(二分,SA,线段树)
热门文章
- LeetCode 1022. 从根到叶的二进制数之和(递归)
- LeetCode 127. 单词接龙(图的BFS/双向BFS)
- google 浏览器默认打开控制台_前端开发调试:浏览器console方法总结
- python模式匹配算法_详解Python 最短匹配模式
- 怎么读取matlab程序包,Nifti程序包,用于写入,读取和处理医学影像,适用于MATLAB
- python语言支持函数式编程_Python语言之Pyhton入门笔记函数式编程
- 限定域文本语料的短语挖掘(Phrase Mining)
- 开源开放 | DeepKE发布新版本:支持低资源、长篇章、多任务的图谱抽取开源框架(浙江大学)...
- Android自动化测试探索
- Android如何给无法更改继承关系的Activity更换ActionBar(setContentView方法实战)