数据结构的作业QAQ
二叉树:输出所有从根到叶子的路径的算法
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
typedef struct Node {
char val;
Node *lch,*rch;
Node(char val='#'):val(val),lch(NULL),rch(NULL){}
}*TNode;
void build(TNode &T) {
char c; cin >> c;
if (c == '#') return;
T = new Node(c);
build(T->lch);
build(T->rch);
}
char ans[100];
void Out(TNode T,int dep) {
ans[dep] = T->val;
if (T->lch==NULL && T->rch==NULL) {
ans[dep+1] = '\0';
printf("%s\n",ans);
return;
}
if (T->lch) Out(T->lch,dep+1);
if (T->rch) Out(T->rch,dep+1);
}
int tp;
void Out(TNode T) {
ans[tp++] = T->val;
if (T->lch==NULL && T->rch==NULL) {
ans[tp] = '\0';
printf("%s\n",ans);
}
if (T->lch) Out(T->l