首页 > 代码库 > 剑指offer系列源码-从上往下打印二叉树

剑指offer系列源码-从上往下打印二叉树

从上往下打印出二叉树的每个节点,同层节点从左至右打印。

#include <iostream>
#include<stdio.h>
#include<deque>
using namespace std;
struct BinaryTreeNode{
    int value;
    BinaryTreeNode* left;
    BinaryTreeNode* right;
};
void printFromTopToBottom(BinaryTreeNode* root){
    if(!root){
        return;
    }
    //双向队列
    deque<BinaryTreeNode*> dequeTreeNode ;
    dequeTreeNode.push_back(root);
    while(dequeTreeNode.size()){
        BinaryTreeNode* pNode = dequeTreeNode.front();
        dequeTreeNode.pop_front();
        printf("%d ",pNode->value);
        if(pNode->left){
            dequeTreeNode.push_back(pNode->left);
        }
        if(pNode->right){
            dequeTreeNode.push_back(pNode->right);
        }
    }

}
int main(){
    return 0;
}


剑指offer系列源码-从上往下打印二叉树