#include <deque>
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
using namespace std;
struct BinaryTreeNode
{
int m_value;
BinaryTreeNode* m_pLeft;
BinaryTreeNode* m_pRight;
}*TPP,TPPP;
void PrintFromToptoBottom(BinaryTreeNode* pTreeRoot)
{
if(pTreeRoot == NULL)
return;
BinaryTreeNode *pCurrent;// = pTreeRoot;
deque<BinaryTreeNode*> p;
p.push_back(pTreeRoot);
while(p.size())
{
pCurrent = p.front();
p.pop_front();
cout<<pCurrent->m_value<<" ";
if(pCurrent->m_pLeft)
{
p.push_back(pCurrent->m_pLeft);
}
if(pCurrent->m_pRight)
{
p.push_back(pCurrent->m_pRight);
}
}
}
void CreateBinaryTree(BinaryTreeNode* &T)
{
int value;
cin>>value;
if(value == -1)
return;
else
{
BinaryTreeNode *t;
t=(BinaryTreeNode*)malloc(sizeof(BinaryTreeNode));
t->m_value=value;
t->m_pLeft=NULL;
t->m_pRight=NULL;
T=t;
CreateBinaryTree(T->m_pLeft);
CreateBinaryTree(T->m_pRight);
}
}
void inorder(BinaryTreeNode * &pTree)
{
if(pTree)
{
cout<<pTree->m_value<<" ";
inorder(pTree->m_pLeft);
inorder(pTree->m_pRight);
}
}
int main()
{
BinaryTreeNode *pTree = NULL;
CreateBinaryTree(pTree);
inorder(pTree);
PrintFromToptoBottom(pTree);
}
面试题23:从上往下打印二叉树
最新推荐文章于 2021-05-19 18:57:52 发布