94. 二叉树的中序遍历
typedef struct tree{
char Data;
struct tree *Left;
struct tree *Right;
}*BinTree;
void InOrderTraversal1(BinTree BT)
{
if(BT){
InOrderTraversal1(BT->Left);
printf("%d\n", BT->Data);
InOrderTraversal1(BT->Right);
}
}
//非递归
void InOrderTraversal2(BinTree BT)
{
BinTree T = BT;
BinTree Stack[101];
int top = 0, book[101] = {0};
while(T || !Stack[top]){
while(T){
book[top] = 1;
Stack[top] = T;
top ++;
T = T->Left;
}
if(book[top]){
T = Stack[top];
book[top] = 0;
top --;
printf("%d\n", T->Data);
T = T->Right;
}
}
}
144. 二叉树的前序遍历
//递归
void PreOrderTraversal1(BinTree BT)
{
if(BT){
printf("%d", BT->Data);
PreOrderTraversal1(BT->Left);
PreOrderTraversal1(BT->Right);
}
}
//非递归
void PreOrderTraversal2(BinTree BT)
{
BinTree T = BT;
BinTree Stack[101];
int top = 0, book[101] = {0};
while(T || book[top]){
while(T){
book[top] = 1;
Stack[top] = T;
top ++;
printf("%d\n", T->Data);
T = T->Left;
}
if(book[top]){
T = Stack[top];
book[top] = 0;
top --;
T = T->Right;
}
}
}