# 二叉樹的層級順序遍歷

## 二叉樹的層次順序遍歷的實現

```/*C++ Implementation to prin the BFS of given binary tree*/
#include<bits/stdc++.h>
using namespace std;
/*Structure of Node of BT which contain pointer to left child and right child and a data for node.*/
struct Node{
int data;
struct Node* left;// for left child;
struct Node* right;// for right child;
Node(int value)// create a node using new Node;
{
data=value;
left=NULL;
right=NULL;
}
};
/*Function which print level order of the given tree*/
void level_order(Node* root)
{
if(root==NULL)
{
return;
}
queue<Node*> q;
q.push(root);
while(!q.empty())
{
Node* temp=q.front();
cout<<temp->data<<" ";
q.pop();
if(temp->left!=NULL)
{
q.push(temp->left);
}
if(temp->right!=NULL)
{
q.push(temp->right);
}
}
cout<<endl;
}
int main()
{
/*construct tree*/
Node* root= new Node(0);//root node;
root->left= new Node(1);
root->right= new Node(2);
root->left->left= new Node(3);
root->left->right= new Node(4);
root->right->left= new Node(5);
root->right->right= new Node(6);
cout<<"Level order traversal of BT: ";
level_order(root);
return 0;
}```
`Level order traversal of BT: 0 1 2 3 4 5 6`

## 空間複雜度

O（最大水平） 其中max_level表示二進制的任何級別中的最大元素數 .