题目:从上往下打印出二叉树的每个结点,同一层的结点按照从左到右的顺序打印。例如输入如图的二叉树,则依次打印出8,6,10,5,7,9,11.(其实是按层遍历)
二叉树结点的定义如下:
struct BinaryTreeNode{
       int     m_nValue;
       BinaryTreeNode*     m_pLeft;
       BinaryTreeNode*     m_pRight;
}

代码实现:

package com.yyq;

import java.util.LinkedList;
import java.util.Queue; /**
* Created by Administrator on 2015/9/16.
*/
public class PrintFromTopToBottom {
public static void printFromTopToBottom(BinaryTreeNode pTreeRoot){
if (pTreeRoot == null)
return;
Queue<BinaryTreeNode> queue = new LinkedList<BinaryTreeNode>();
queue.offer(pTreeRoot);
while (queue.size() > 0){
BinaryTreeNode pNode = queue.poll();
System.out.print(pNode.getM_nValue() + "\t");
if (pNode.getM_pLeft() != null){
queue.offer(pNode.getM_pLeft());
}
if (pNode.getM_pRight() != null){
queue.offer(pNode.getM_pRight());
}
}
}
// ====================测试代码====================
public static void Test(String testName, BinaryTreeNode pRoot){
if (testName != null)
System.out.println(testName + " Begin:");
printFromTopToBottom(pRoot);
System.out.println();
}
// 测试完全二叉树:除了叶子节点,其他节点都有两个子节点
// 8
// 6 10
// 5 7 9 11
public static void Test1() {
System.out.println("\n=====Test1 starts:=====");
BinaryTreeNode pNode8 = new BinaryTreeNode(8);
BinaryTreeNode pNode6 = new BinaryTreeNode(6);
BinaryTreeNode pNode10 = new BinaryTreeNode(10);
BinaryTreeNode pNode5 = new BinaryTreeNode(5);
BinaryTreeNode pNode7 = new BinaryTreeNode(7);
BinaryTreeNode pNode9 = new BinaryTreeNode(9);
BinaryTreeNode pNode11 = new BinaryTreeNode(11); pNode8.connectTreeNodes(pNode6, pNode10);
pNode6.connectTreeNodes(pNode5, pNode7);
pNode10.connectTreeNodes(pNode9, pNode11); Test("Test1",pNode8);
pNode8 = null;
} // 测试二叉树:出叶子结点之外,左右的结点都有且只有一个左子结点
// 8
// 7
// 6
// 5
//
public static void Test2() {
System.out.println("\n=====Test2 starts:=====");
BinaryTreeNode pNode8 = new BinaryTreeNode(8);
BinaryTreeNode pNode7 = new BinaryTreeNode(7);
BinaryTreeNode pNode6 = new BinaryTreeNode(6);
BinaryTreeNode pNode5 = new BinaryTreeNode(5);
BinaryTreeNode pNode4 = new BinaryTreeNode(4); pNode8.connectTreeNodes(pNode7, null);
pNode7.connectTreeNodes(pNode6, null);
pNode6.connectTreeNodes(pNode5, null);
pNode5.connectTreeNodes(pNode4, null); Test("Test2",pNode8);
pNode8 = null;
} // 测试二叉树:出叶子结点之外,左右的结点都有且只有一个右子结点
// 8
// 7
// 6
// 5
// 4
public static void Test3() {
System.out.println("\n=====Test3 starts:=====");
BinaryTreeNode pNode8 = new BinaryTreeNode(8);
BinaryTreeNode pNode7 = new BinaryTreeNode(7);
BinaryTreeNode pNode6 = new BinaryTreeNode(6);
BinaryTreeNode pNode5 = new BinaryTreeNode(5);
BinaryTreeNode pNode4 = new BinaryTreeNode(4); pNode8.connectTreeNodes(null, pNode7);
pNode7.connectTreeNodes(null, pNode6);
pNode6.connectTreeNodes(null, pNode5);
pNode5.connectTreeNodes(null, pNode4); Test("Test3",pNode8);
pNode8 = null;
} // 测试空二叉树:根结点为空指针
public static void Test4() {
System.out.println("\n=====Test4 starts:=====");
BinaryTreeNode pNode = null; Test("Test4",pNode);
} // 测试只有一个结点的二叉树
public static void Test5() {
System.out.println("=====Test5 starts:=====");
BinaryTreeNode pNode8 = new BinaryTreeNode(8); Test("Test5",pNode8);
pNode8 = null;
} public static void main(String[] args) {
Test1();
Test2();
Test3();
Test4();
Test5();
}
}
 
结果输出:
=====Test1 starts:=====
Test1 Begin:
8 6 10 5 7 9 11
 
=====Test2 starts:=====
Test2 Begin:
8 7 6 5 4
 
=====Test3 starts:=====
Test3 Begin:
8 7 6 5 4
 
=====Test4 starts:=====
Test4 Begin:
 
=====Test5 starts:=====
Test5 Begin:
8

P137、面试题23:从上往下打印二叉树的更多相关文章

  1. 剑指Offer:面试题23——从上往下打印二叉树(java实现)

    问题描述: 从上往下打印出二叉树的每个节点,同层节点从左至右打印. 思路: 按照层次遍历的方法,使用队列辅助. 1.将根结点加入队列. 2.循环出队,打印当前元素,若该结点有左子树,则将其加入队列,若 ...

  2. 剑指offer-面试题23.从上往下打印二叉树

    题目:从上往下打印出二叉树的每个结点,同一层的结点按照从左到右的顺序打印.例如输入图4.5中 的二叉树,则依次打印出8.6.10.5.7.9.11二叉树结点的定义如下: struct BinaryTr ...

  3. 《剑指offer》面试题23 从上往下打印二叉树 Java版

    注意层序遍历的时候对每一层的处理方式可能不同,这里把每一层的元素保存进一个List中了,那么就需要记录每一层的数量. public List<List<Integer>> se ...

  4. 面试题23从上到下打印二叉树+queue操作

    //本题思路就是层次遍历二叉树,使用一个队列来模拟过程 /* struct TreeNode { int val; struct TreeNode *left; struct TreeNode *ri ...

  5. 剑指offer_面试题_从上往下打印二叉树

    题目:从上往下打印出二叉树的每一个结点.同一层的结点依照从左到右的顺序打印.比如输入图4.5中的二叉树.则依次打印出8.6.10.5.7.9.11. 8 /     \ 6     10 /   \ ...

  6. C++版 - 剑指offer 面试题23:从上往下打印二叉树(二叉树的层次遍历BFS) 题解

    剑指offer  面试题23:从上往下打印二叉树 参与人数:4853  时间限制:1秒  空间限制:32768K 提交网址: http://www.nowcoder.com/practice/7fe2 ...

  7. 【剑指offer 面试题23】从上往下打印二叉树

    思路: 没啥好说的,BFS. C++: #include <iostream> #include <queue> using namespace std; struct Tre ...

  8. 【剑指Offer学习】【面试题23:从上往下打印二叉树】

    题目:从上往下打印出二叉树的每一个结点,同一层的结点依照从左向右的顺序打印. 二叉树结点的定义: public static class BinaryTreeNode { int value; Bin ...

  9. 面试题32 - III. 从上到下打印二叉树 III

    面试题32 - III. 从上到下打印二叉树 III 请实现一个函数按照之字形顺序打印二叉树,即第一行按照从左到右的顺序打印,第二层按照从右到左的顺序打印,第三行再按照从左到右的顺序打印,其他行以此类 ...

  10. 【剑指Offer】面试题32 - III. 从上到下打印二叉树 III

    题目 请实现一个函数按照之字形顺序打印二叉树,即第一行按照从左到右的顺序打印,第二层按照从右到左的顺序打印,第三行再按照从左到右的顺序打印,其他行以此类推. 例如: 给定二叉树: [3,9,20,nu ...

随机推荐

  1. Poj 2109 / OpenJudge 2109 Power of Cryptography

    1.Link: http://poj.org/problem?id=2109 http://bailian.openjudge.cn/practice/2109/ 2.Content: Power o ...

  2. Linux C 程序 字符串函数(12)

    字符串函数C语言的字符串处理函数1.puts函数 //把一个以'\0'结尾的字符串输出到屏幕 char a[] = "Welcome to"; char *p = "Li ...

  3. 开发问题记录——ArcEngine问题记录

    ArcEngine 使用Winform进行坐标投影变换,用到AE空间,出现如下错误:   “ESRI.ArcGIS.esriSystem.IXMLSerialize”在未被引用的程序集中定义.必须添加 ...

  4. SQL Server数据库连接类SQLHelper.cs

    using System; using System.Collections.Generic; using System.Text; using System.Configuration; using ...

  5. object-c实现的 在PHP中oauth加密算法

    说起这个算法,在php中我是这么实现的 function generateSig ($params, $secret = '') {     if (empty($secret)) {         ...

  6. html lang

    目前,语言的标签表示法的国际标准是RFC 4646,名称是<Tags for Identifying Languages>.简单说,这个文件规定,一种语言的标签应该按照如下方式排列: la ...

  7. 浅谈String类型

    首先,我们要知道的是String类型是一个引用类型,它的基类是Object.并且它的内容是只读的. 我们有时候经常会看到两个字符串类型,一个是“Sting”,一个是“string”.大写的String ...

  8. buffer busy wait在RAC环境下出现

    昨天运维组的同时反映有套系统用户反映很慢,需要协助帮忙检查什么原因引起的性能问题.导出了从8点到11点的AWR报告进行分析,发现等待事件里大部分的指标都正常,就是buffer busy wait的平均 ...

  9. 泛形集合List<T>

    public class Person { /// <summary> /// 姓名 /// </summary> private string name; public st ...

  10. javascript高级编程笔记06(面相对象2)

    1)  构造函数模式 es中的构造函数可以用来创建特定类型的对象,像Object和Array这样的原生构造函数,在运行时会自动出现在执行环境中,此外,也可以创建自定义的构造函数,从而定义自定义对象类型 ...