/**
*2018.10.25 21:32
*二叉树中求根节点到每个叶子结点的路径
*/
#include<stdio.h>
#define MAX 100
typedef char Elem;
typedef struct BTNode{
Elem e;
struct BTNode* lchild;
struct BTNode* rchild;
}BTNode;
void rootToLeafPath(BTNode* T);
int main(void) {
putchar('\n');
system("pause");
return 0;
}
Elem path[MAX], top = -1;
void rootToLeafPath(BTNode* T) {
if (T != NULL) {
path[++top] = T->e;
if (T->lchild == NULL && T->rchild == NULL) {
//从根到叶
for (int i = 0; i <= top; ++i)
printf("%c", path[i]);
/*从叶到根
int i = top;
while (i > -1) {
printf("%c", path[i]);
--i;
}*/
}
rootToLeafPath(T->lchild);
rootToLeafPath(T->rchild);
--top;
}
}
二叉树中求根节点到每个叶子结点的路径
最新推荐文章于 2020-05-15 22:24:22 发布