用c语言完整写出二叉树三种遍历
时间: 2023-06-02 21:03:44 浏览: 114
答:好的,以下是三种二叉树遍历的完整C语言代码:
先序遍历:
void preOrderTraversal(Node* root) {
if (root == NULL) {
return;
}
printf("%d ", root->data);
preOrderTraversal(root->left);
preOrderTraversal(root->right);
}
中序遍历:
void inOrderTraversal(Node* root) {
if (root == NULL) {
return;
}
inOrderTraversal(root->left);
printf("%d ", root->data);
inOrderTraversal(root->right);
}
后序遍历:
void postOrderTraversal(Node* root) {
if (root == NULL) {
return;
}
postOrderTraversal(root->left);
postOrderTraversal(root->right);
printf("%d ", root->data);
}
相关问题
用c语言写一个二叉树的先序遍历
二叉树的先序遍历可以使用递归或非递归方式实现。这里给出一个使用递归方式实现的C语言代码:
```c
#include <stdio.h>
#include <stdlib.h>
typedef struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
} TreeNode;
void preorderTraversal(TreeNode* root) {
if (root == NULL) {
return;
}
printf("%d ", root->val);
preorderTraversal(root->left);
preorderTraversal(root->right);
}
int main() {
// 构造一个二叉树
TreeNode* root = (TreeNode*)malloc(sizeof(TreeNode));
root->val = 1;
root->left = (TreeNode*)malloc(sizeof(TreeNode));
root->left->val = 2;
root->left->left = NULL;
root->left->right = NULL;
root->right = (TreeNode*)malloc(sizeof(TreeNode));
root->right->val = 3;
root->right->left = (TreeNode*)malloc(sizeof(TreeNode));
root->right->left->val = 4;
root->right->left->left = NULL;
root->right->left->right = NULL;
root->right->right = (TreeNode*)malloc(sizeof(TreeNode));
root->right->right->val = 5;
root->right->right->left = NULL;
root->right->right->right = NULL;
// 先序遍历
preorderTraversal(root);
printf("\n");
return 0;
}
```
这段代码定义了一个`TreeNode`结构体表示二叉树节点,其中包含节点的值`val`和左右子节点指针`left`和`right`。`preorderTraversal`函数接受一个二叉树的根节点指针,如果根节点为空则直接返回,否则先输出根节点的值,然后递归遍历左子树和右子树。在`main`函数中构造了一个二叉树,并对其进行先序遍历。
用c语言写一个二叉树的层序遍历
当然可以,以下是代码:
```c
#include <stdio.h>
#include <stdlib.h>
// 二叉树结构体
typedef struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
} TreeNode;
// 队列结构体
typedef struct Queue {
TreeNode **data;
int front, rear, size;
} Queue;
// 创建队列
Queue *createQueue(int size) {
Queue *q = (Queue *)malloc(sizeof(Queue));
q->data = (TreeNode **)malloc(sizeof(TreeNode *) * size);
q->front = q->rear = 0;
q->size = size;
return q;
}
// 入队
void enqueue(Queue *q, TreeNode *node) {
if ((q->rear + 1) % q->size == q->front) return;
q->data[q->rear] = node;
q->rear = (q->rear + 1) % q->size;
}
// 出队
TreeNode *dequeue(Queue *q) {
if (q->front == q->rear) return NULL;
TreeNode *node = q->data[q->front];
q->front = (q->front + 1) % q->size;
return node;
}
// 层序遍历
void levelOrder(TreeNode *root) {
if (!root) return;
Queue *q = createQueue(100);
enqueue(q, root);
while (q->front != q->rear) {
int size = q->rear - q->front;
for (int i = 0; i < size; i++) {
TreeNode *node = dequeue(q);
printf("%d ", node->val);
if (node->left) enqueue(q, node->left);
if (node->right) enqueue(q, node->right);
}
printf("\n");
}
}
int main() {
// 创建二叉树
TreeNode *root = (TreeNode *)malloc(sizeof(TreeNode));
root->val = 1;
root->left = (TreeNode *)malloc(sizeof(TreeNode));
root->left->val = 2;
root->left->left = NULL;
root->left->right = NULL;
root->right = (TreeNode *)malloc(sizeof(TreeNode));
root->right->val = 3;
root->right->left = (TreeNode *)malloc(sizeof(TreeNode));
root->right->left->val = 4;
root->right->left->left = NULL;
root->right->left->right = NULL;
root->right->right = (TreeNode *)malloc(sizeof(TreeNode));
root->right->right->val = 5;
root->right->right->left = NULL;
root->right->right->right = NULL;
// 层序遍历
levelOrder(root);
return 0;
}
```
输出结果为:
```
1
2 3
4 5
```
这是二叉树的层序遍历结果。
阅读全文