从上往下打印出二叉树的每个节点,同层节点从左至右打印。
#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; }
时间: 2024-10-07 12:32:35