forked from imxtx/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtraversal_recursive.c
70 lines (61 loc) · 1.66 KB
/
traversal_recursive.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <stdio.h>
#include "../utils.h"
/**
* @brief 先序遍历(递归版本)
*
* @param root 根节点
*/
void preOrderRecursive(treeNode *root)
{
if (NULL == root) return; // root不为NULL才继续访问
printf("%d ", root->data); // 对数据进行访问
preOrderRecursive(root->left); // 访问左子树
preOrderRecursive(root->right); // 访问右子树
}
/**
* @brief 中序遍历(递归版本)
*
* @param root 根节点
*/
void inOrderRecursive(treeNode *root)
{
if (NULL == root) return; // root不为NULL才继续访问
inOrderRecursive(root->left); // 访问左子树
printf("%d ", root->data); // 对数据进行访问
inOrderRecursive(root->right); // 访问右子树
}
/**
* @brief 后序遍历(递归版本)
*
* @param root 根节点
*/
void postOrderRecursive(treeNode *root)
{
if (NULL == root) return; // root不为NULL才继续访问
postOrderRecursive(root->left); // 访问左子树
postOrderRecursive(root->right); // 访问右子树
printf("%d ", root->data); // 对数据进行访问
}
// 测试函数是否工作正常
void test()
{
// 由数组创建二叉树, -1表示节点不存在
int arr[] = {1, 2, 3, -1, 4, 5, 6, -1, -1, 7};
int len = NELEMS(arr);
treeNode *root = createTreeFromArray(arr, len);
// 测试三种遍历方法
printf("先序:");
preOrderRecursive(root);
putchar('\n');
printf("中序:");
inOrderRecursive(root);
putchar('\n');
printf("后序:");
postOrderRecursive(root);
putchar('\n');
}
int main(int argc, char const *argv[])
{
test();
return 0;
}