文章
105
粉丝
69
获赞
117
访问
61.3k
#include<bits/stdc++.h>
using namespace std;
int cnt;
string str;
typedef struct Tree{
char v;
Tree *l, *r;
}Tree;
Tree *build() //建树
{
char c = str[cnt ++];
if(c == '0') return NULL;
Tree *root = new Tree();
root -> v = c;
root -> l = build();
root -> r = build();
return root;
}
void traversal_piror(Tree *root) //前序遍历
{
if(!root) return;
printf("%c ", root -> v);
traversal_piror(root -> l);
traversal_piror(root -> r);
}
void traversal_mid(Tree *root) //中序遍历
{
if(!root) return;
traversal_mid(root -> l);
printf("%c ", root -> v);
traversal_mid(root -> r);
}
void traversal_rear(Tree *root)//后序遍历
{
if(!root) return;
traversal_rear(root -> l);
traversal_rear(root -> r);
printf("%c ", root -> v);
}
void get_ans(Tree *root) //层序遍历求叶子结点个数
{
int ans = 0;
queue<Tree*> q;
if(root) q.push(root);
while(q.size())
{
int n = q.siz...
登录后发布评论
暂无评论,来抢沙发