剑指offer-第四章解决面试题的思路(从上往下打印二叉树)
题目:从上往下打印二叉树的每一个节点,同一层的节点按照从左到右的顺序打印
思路:这是一个层序遍历的问题,因此要借用到队列。我们可以在打印第一个节点的同时将这个节点的左右子节点都放入队列,同样打印左右子树。
抽象的问题具体化:


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);
} }
剑指offer-第四章解决面试题的思路(从上往下打印二叉树)的更多相关文章
- 《剑指offer》第三十二题(分行从上到下打印二叉树)
// 面试题32(二):分行从上到下打印二叉树 // 题目:从上到下按层打印二叉树,同一层的结点按从左到右的顺序打印,每一层 // 打印到一行. #include <cstdio> #in ...
- 剑指offer第四章
剑指offer第四章 1.二叉树的镜像 二叉树的镜像:输入一个二叉树,输出它的镜像 分析:求树的镜像过程其实就是在遍历树的同时,交换非叶结点的左右子结点. 求镜像的过程:先前序遍历这棵树的每个结点,如 ...
- C++版 - 剑指offer 面试题23:从上往下打印二叉树(二叉树的层次遍历BFS) 题解
剑指offer 面试题23:从上往下打印二叉树 参与人数:4853 时间限制:1秒 空间限制:32768K 提交网址: http://www.nowcoder.com/practice/7fe2 ...
- 剑指offer-第四章解决面试题的思路(栈的压入和弹出序列)
题目:输入两个整数序列,第一个序列表示栈的压入序列,请判断第二个序列是否为弹出序列. 思路:定义两个指针sPush和sPop分别指向两个整数序列的开头,借助一个辅助的栈,将第一个序列的数据依次压入栈中 ...
- 剑指offer-第四章解决面试题的思路(包含min函数的栈)
题目:定义栈的数据结构,请在该类型中实现一个能够得到栈的最小元素的min函数,在该栈中,调用min,push及pop的时间复杂度都是O(1) 思路:定义两个栈分别为dataStack和minStack ...
- 剑指offer-第四章解决面试题的思路(顺序打印矩阵)
题目:输入一个矩阵,按照从外向里的顺序依次打印出每一个数.(画图让抽象的问题形象化) 思路:打印矩阵时,把每一层当做一个圈来打印,找到打印整个矩阵的截止条件. 从上图中我可以看到一个6*6的矩阵(长宽 ...
- 剑指offer-第四章解决面试题的思路(二叉树的镜像)
题目:请完成函数,输入一个二叉树,该函数输出它的镜像. 思路:可能没有听说过书的镜像,但是可以通过画图等来找灵感.就像照镜子一样,人的左边和右边交换了. 如图: 通过如下图变化就可以由左图得到右图: ...
- 剑指Offer面试题:21.从上到下打印二叉树
一.题目:从上到下打印二叉树 题目:从上往下打印出二叉树的每个结点,同一层的结点按照从左到右的顺序打印.例如输入下图中的二叉树,则依次打印出8.6.10.5.7.9.11. 二叉树节点的定义如下,采用 ...
- (剑指Offer)面试题23:从上到下打印二叉树
题目: 从上往下打印出二叉树的每个节点,同层节点从左至右打印. 思路: 很明显,这是一个广度优先遍历. 需要一个队列容器来保存结点,具体操作: 1.将根结点压入队列中,并打印根结点:如果根结点有子结点 ...
随机推荐
- node-inspector使用方法
开发node.js程序使用的是javascript语言,其中最麻烦的还是调试,这里介绍一下node-inspector使用方法.具体资料可以看参考资料中的GITHUB文档. 方法/步骤 使用命令$ ...
- UILable 的 属性设置
//UILable的大小自适应实例 UILabel *myLable = [[UILabel alloc] initWithFrame:CGRectMake(, , , )];//设定位置与大小 [m ...
- ==与equals的各种情况
== 能用于基本类型之间.基本类型与引用类型之间及相同引用类型之间,不能用于不同引用类型之间 对于基本类型,取值来对比,对于引用类型,取地址来对比 int a= 1; Integer b= 1; Sy ...
- 手动漏洞挖掘-SQL注入(安全牛笔记)
substring_index(USER(),"@",l)-- #是将查询出来的结果进行切分,以@符号的方式切分 ’ union select table_name,table_s ...
- Convolutional Neural Network
Why CNN for Image 图片是由像素点组成的,可以这样来解释深度神经网络对图片的处理. 第一层的layer是最基本的分类器,区分一些基本的特征,比如颜色.是否有斜线. 第二层的layer会 ...
- nginx反向代理服务器端口问题
nginx可以很方便的配置成反向代理服务器 server { listen 80; server_name bothlog.com; location / { proxy_set_header H ...
- Shiro缓存使用Redis、Ehcache、自带的MpCache实现的三种方式实例
第一种:使用Redis做缓存,将数据存储到redis数据库中 第一步:在项目里面引入redis,配置文件如下: 配置文件:spring_shiro_redis.xml <?xml version ...
- PasswordHasher 算法
public override PasswordVerificationResult VerifyHashedPassword(string hashedPassword, string provid ...
- Spring @Transactional (一)
Spring @Transactional (一) 博客分类: JAVA SpringJPAJDBCUPSQL Spring事务的传播行为 在service类前加上@Transactional,声明 ...
- UniquePaths,UniquePaths2,路径问题。动态规划。
UniquePaths:给定m*n矩阵,从(0,0)到(m-1,n-1)路径条数.只能向下向右走. 算法分析:这和爬楼梯问题很像,到(m,n)的路径数是到(m-1,n)和(m,n-1)路径和.第一行, ...