题目:从上往下打印二叉树的每一个节点,同一层的节点按照从左到右的顺序打印

思路:这是一个层序遍历的问题,因此要借用到队列。我们可以在打印第一个节点的同时将这个节点的左右子节点都放入队列,同样打印左右子树。

抽象的问题具体化:

C++代码:

#include<iostream>
#include<deque>
using namespace std;
struct BinaryTreeNode
{
int m_nValue;
BinaryTreeNode* m_pLeft;
BinaryTreeNode* m_pRight;
};
BinaryTreeNode* constructCore(int* preOrderStart,int* preOrderEnd,int* inOrderStart,int* inOrderEnd)
{
int value=*preOrderStart;
BinaryTreeNode* root=new BinaryTreeNode();
root->m_nValue=value;
root->m_pLeft=root->m_pRight=NULL;
//当只有一个节点时
if(preOrderStart==preOrderEnd)
{
if(inOrderStart==inOrderEnd&&*preOrderStart==*inOrderStart)
return root;
else
throw std::exception("inVaild input");
}
//当有多个节点的时候
int* rootInOrder=inOrderStart;
while(rootInOrder<inOrderEnd&&*rootInOrder!=value)
{
rootInOrder++;
}
if(rootInOrder==inOrderEnd&&*rootInOrder!=value)
throw std::exception("inVaild input"); int leftLength=rootInOrder-inOrderStart;
int rightLength=inOrderEnd-rootInOrder;
if(leftLength>)
root->m_pLeft=constructCore(preOrderStart+,preOrderStart+leftLength,inOrderStart,inOrderStart+leftLength-);
if(rightLength>)
root->m_pRight=constructCore(preOrderStart+leftLength+,preOrderEnd,inOrderStart+leftLength+,inOrderEnd);
return root;
}
BinaryTreeNode* construct(int * preOrder,int* inOrder,int length)
{
if(preOrder==NULL||inOrder==NULL||length<)
return NULL;
return constructCore(preOrder,preOrder+length-,inOrder,inOrder+length-);
}
void printNode(BinaryTreeNode* pNode)
{
if(pNode==NULL)
return;
cout<<"this node is: "<<pNode->m_nValue<<endl;
if(pNode->m_pLeft!=NULL)
cout<<"the left node is: "<<pNode->m_pLeft->m_nValue<<endl;
else
cout<<"the left node is NULL"<<endl;
if(pNode->m_pLeft!=NULL)
cout<<"the right node is: "<<pNode->m_pRight->m_nValue<<endl;
else
cout<<"the right node is NULL"<<endl;
}
void printBiTree(BinaryTreeNode* root)
{
if(root==NULL)
return;
printNode(root);
if(root->m_pLeft!=NULL)
printBiTree(root->m_pLeft);
if(root->m_pRight!=NULL)
printBiTree(root->m_pRight);
}
void main()
{
int a[]={,,};
int b[]={,,};
BinaryTreeNode* pHead=construct(a,b,);
printBiTree(pHead);
}

Java代码:

import java.util.LinkedList;
import java.util.Queue;
public class PrintFromTopToBottom { public static class BinaryTreeNode{
int m_nValue;
BinaryTreeNode m_pLeft;
BinaryTreeNode m_pRight; }
public static BinaryTreeNode CreateBiTree(int[] preorder,int start,int[] inorder,int end,int length){
if(preorder==null||inorder==null||preorder.length!=inorder.length||length<0)
return null;
BinaryTreeNode pRoot=new BinaryTreeNode();
int value=preorder[start];
pRoot.m_nValue=value;
pRoot.m_pLeft=pRoot.m_pRight=null;
//只有一个节点的时候
if(length==1){
if(preorder[start]==inorder[end])
return pRoot;
else
throw new RuntimeException("inVaild input!");
}
//有多个节点的时候
int i=0;
while(i<length){
if(value==inorder[end-i])
break;
i++;
}
if(i==length)
throw new RuntimeException("inVaild input!");
pRoot.m_pLeft=CreateBiTree(preorder,start+1,inorder,end-i-1,length-i-1);
pRoot.m_pRight=CreateBiTree(preorder,start+length-i,inorder,end,i);
return pRoot;
} public static void printFromTopToBottom(BinaryTreeNode pHead){
if(pHead==null)
return;
Queue<BinaryTreeNode> queue=new LinkedList<BinaryTreeNode>();
queue.add(pHead);
while(!queue.isEmpty()){
BinaryTreeNode pNode=queue.poll(); System.out.println(pNode.m_nValue);
if(pNode.m_pLeft!=null)
queue.add(pNode.m_pLeft);
if(pNode.m_pRight!=null)
queue.add(pNode.m_pRight);
}
}
public static void main(String[] args)
{
int[] a={3,5,6};
int[] b={5,3,6};
BinaryTreeNode pHead=CreateBiTree(a,0,b,2,a.length);
printFromTopToBottom(pHead);
} }

最新文章

  1. PHP基础之PDO
  2. MFC-01-Chapter01:Hello,MFC---1.3 第一个MFC程序(03)
  3. HackerRank &quot;Larry&#39;s Array&quot;
  4. Spring3系列11- Spring AOP——自动创建Proxy
  5. android 进程间通信数据(二)------parcel的实现
  6. 130道ASP.NET面试题
  7. maltab几个常见的问题
  8. DataGrid 使用模型列后实现点击列名称排序
  9. linux下如何使用vnstat查看服务器带宽流量统计
  10. javaEE开发中使用session同步和token机制来防止并发重复提交
  11. Error: Cannot find module &#39;gulp-clone&#39;问题的解决
  12. TP3.2二级导航与高亮显示
  13. JAVA面试一
  14. TypeError: unorderable types: str() &gt;= int()
  15. nginx缓存设置(expires)
  16. Web概述
  17. Centos7.3 之mysql5.7二进制安装
  18. javascript的常用事件
  19. wpf(布局与Canvas )
  20. 【做题】CF119D. String Transformation——KMP

热门文章

  1. 【leetcode刷题笔记】Convert Sorted Array to Binary Search Tree
  2. vue2.0 transition 多个元素嵌套使用过渡
  3. linux驱动调试--修改系统时钟终端来定位僵死问题【转】
  4. Linux系统下配置squid代理服务器的过程详解
  5. python部署LNMP业务服务环境
  6. NO.4 Android开发中常用框架及工具
  7. JavaWeb -- Struts2 ResultType细化, 国际化
  8. Pandas注意事项&窍门
  9. spring3: AOP 之代理机制
  10. Selenium with Python 010 - unittest 框架(又称PyUnit 框架)