【发布时间】:2020-02-28 09:56:36
【问题描述】:
如何以这种方式打印我的二叉树?
the original tree I need
5 5
/ \
2 9 2 9
/ \ / \
0 3 7 12 0 3 7 12
我的打印功能是:
void display(struct node* start){
cout << start->data << " ";
if(start != NULL){
if(start->left){
cout << "\n";
display(start->left);
}
if(start->right) {
display(start->right);
}
}
}
目前我的函数以这种方式打印
5
2
0 3 9
7 12
【问题讨论】:
-
树总是完整的还是缺少分支?
-
您需要对树进行级别顺序遍历(或 BFS)。你正在做的是DFS。您应该使用队列进行遍历而不是递归。
-
@Botje 树已完成
标签: c++ data-structures binary binary-tree