來自劍指offer 23題:從上往下打印二叉樹责鳍。
這里是利用隊列來保證層序從上到下琐脏,從左到右激蹲。
/*
* 二叉樹的層次遍歷
*/
#include <iostream>
#include <queue>
using namespace std;
struct TreeNode{
int val;
TreeNode* left, *right;
TreeNode(int v = 0) : val(v), left(NULL), right(NULL){}
};
void visit(TreeNode* p){ //訪問函數(shù),這里簡單只設(shè)為是打印
cout << p->val << " ";
}
void levelTravel(TreeNode *root){
if(root == NULL)
return;
queue<TreeNode*> Q;
Q.push(root); //初始化
while(!Q.empty()){
TreeNode* cur = Q.front();
Q.pop();
visit(cur);
if(cur->left) Q.push(cur->left);
if(cur->right) Q.push(cur->right);
}
}
int main(){
TreeNode n0(0), n1(1), n2(2), n3(3), n4(4),n5(5), n6(6), n7(7),n8(8), n9(9),n10(10);
n0.left = &n1; n0.right = &n2;
n1.left = &n3; n1.right = &n4;
n2.left = &n5; n2.right = &n6;
n4.left = &n7; n4.right = &n8;
n5.left = &n9; n5.right = &n10;
levelTravel(&n0);
cout << endl;
}