#include "stdio.h"
#include "malloc.h"
struct Node* current = NULL; //声明的一个全局变量表示的是链表的尾部
//二叉树
struct Node
{
int value;
struct Node* left; //左儿子节点
struct Node* right; //右儿子节点
};
void addLeftNode(struct Node *node, struct Node *left) //添加一个左儿子
{
node->left = left;
}
void addRightNode(struct Node *node, struct Node *right) //添加一个右儿子
{
node->right = right;
}
struct Node* createNode(int value)
{
struct Node* node = (struct Node*)malloc(sizeof(struct Node));
node->value = value;
node->left = NULL;
node->right = NULL;
return node;
}
void firstRoot(struct Node* root) //递归先根遍历
{
printf("%d\n", root->value);
if(root->left != NULL)
firstRoot(root->left);
if(root->right != NULL)
firstRoot(root->right);
}
void middleRoot(struct Node* root) //递归中根遍历
{
if(root->left != NULL)
middleRoot(root->left);
printf("%d\n", root->value);
if(root->right != NULL)
middleRoot(root->right);
}
void lastRoot(struct Node* root) //递归后根遍历
{
if(root->left != NULL)
lastRoot(root->left);
if(root->right != NULL)
lastRoot(root->right);
printf("%d\n", root->value);
}
/*
1
-
2 3 生成树的形状
- -
4 5 6 7
*/
int main()
{
struct Node* first = createNode(1);
addLeftNode(first, createNode(2));
addRightNode(first, createNode(3));
addLeftNode(first->left, createNode(4));
addRightNode(first->left, createNode(5));
addLeftNode(first->right, createNode(6));
addRightNode(first->right, createNode(7));
firstRoot(first);
middleRoot(first);
lastRoot(first);
getchar();
return 0;
}
#include "malloc.h"
struct Node* current = NULL; //声明的一个全局变量表示的是链表的尾部
//二叉树
struct Node
{
int value;
struct Node* left; //左儿子节点
struct Node* right; //右儿子节点
};
void addLeftNode(struct Node *node, struct Node *left) //添加一个左儿子
{
node->left = left;
}
void addRightNode(struct Node *node, struct Node *right) //添加一个右儿子
{
node->right = right;
}
struct Node* createNode(int value)
{
struct Node* node = (struct Node*)malloc(sizeof(struct Node));
node->value = value;
node->left = NULL;
node->right = NULL;
return node;
}
void firstRoot(struct Node* root) //递归先根遍历
{
printf("%d\n", root->value);
if(root->left != NULL)
firstRoot(root->left);
if(root->right != NULL)
firstRoot(root->right);
}
void middleRoot(struct Node* root) //递归中根遍历
{
if(root->left != NULL)
middleRoot(root->left);
printf("%d\n", root->value);
if(root->right != NULL)
middleRoot(root->right);
}
void lastRoot(struct Node* root) //递归后根遍历
{
if(root->left != NULL)
lastRoot(root->left);
if(root->right != NULL)
lastRoot(root->right);
printf("%d\n", root->value);
}
/*
1
-
2 3 生成树的形状
- -
4 5 6 7
*/
int main()
{
struct Node* first = createNode(1);
addLeftNode(first, createNode(2));
addRightNode(first, createNode(3));
addLeftNode(first->left, createNode(4));
addRightNode(first->left, createNode(5));
addLeftNode(first->right, createNode(6));
addRightNode(first->right, createNode(7));
firstRoot(first);
middleRoot(first);
lastRoot(first);
getchar();
return 0;
}