#include<iostream>
#include <algorithm>
#include <queue>
using namespace std;
struct Tree {
Tree* lr, * rr;
char data;
};
void creattree(Tree* &T) {
char op;
cin >> op;
if (op == '#') {
T = NULL;
return;
}
T = new Tree;
T->data = op;
creattree(T->lr);
creattree(T->rr);
}
void f(Tree* t) {
queue<Tree*> q;
if (t==NULL)return;
q.push(t);
while (!q.empty()) {
Tree* a = q.front();
cout << ' ' << a->data;
if (a->lr)q.push(a->lr);
if (a->rr)q.push(a->rr);
q.pop();
}
}
int main() {
Tree *t ;
creattree(t);
f(t);
}
二叉树层次遍历 C++
最新推荐文章于 2024-05-28 21:13:10 发布