二叉树中求根节点到每个叶子结点的路径

/**
*2018.10.25 21:32
*二叉树中求根节点到每个叶子结点的路径
*/
#include

#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;
	}
}

你可能感兴趣的:(数据结构)