面试题23:从上往下打印二叉树

面试题23:从上往下打印二叉树_第1张图片

#include 
#include 
#include 
#include 

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 p;
      p.push_back(pTreeRoot);
      while(p.size())
      {
          pCurrent = p.front();
          p.pop_front();

          cout<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<m_value<<"   ";
        inorder(pTree->m_pLeft);
        inorder(pTree->m_pRight);
    }
}


int main()
{
    BinaryTreeNode *pTree = NULL;

    CreateBinaryTree(pTree);
    inorder(pTree);
    PrintFromToptoBottom(pTree);


}

你可能感兴趣的:(剑指offer)