题目:

typedef struct node{
char data;
node *left;
node *right;
}BTree;
void inorder(BTree tree,int storey){ //tree是结点,storey是当前层数,最高是0
BTree p = tree;
if( p.left!= nullptr || p.right!= nullptr ){ //左右孩子不全为空
if(storey !=0){printf("%s","(");} //不是最外层就套上括号
if(p.left!= nullptr){ //左孩子不为空的话,先遍历左孩子
inorder(*p.left,storey+1); //层数+1
}
}
printf("%c",p.data); //输出根结点内容
if(p.left!= nullptr || p.right!= nullptr) { //左右孩子不全为空
if (p.right != nullptr) { //右孩子不为空的话,遍历右孩子
inorder(*p.right, storey + 1); //层数+1
}
if (storey != 0) { //不是最外层就套上括号
printf("%s", ")");
}
}
}
运行效果:


?
|