二叉树中序遍历C语言实例
时间: 2023-04-02 21:02:03 浏览: 135
当然可以,以下是一个二叉树中序遍历的 C 语言实例:
```c
#include <stdio.h>
#include <stdlib.h>
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
};
void inorderTraversal(struct TreeNode* root){
if(root == NULL){
return;
}
inorderTraversal(root->left);
printf("%d ", root->val);
inorderTraversal(root->right);
}
int main(){
struct TreeNode* root = (struct TreeNode*)malloc(sizeof(struct TreeNode));
root->val = 1;
root->left = NULL;
root->right = (struct TreeNode*)malloc(sizeof(struct TreeNode));
root->right->val = 2;
root->right->left = (struct TreeNode*)malloc(sizeof(struct TreeNode));
root->right->left->val = 3;
root->right->left->left = NULL;
root->right->left->right = NULL;
root->right->right = NULL;
inorderTraversal(root);
return ;
}
```
以上代码实现了一个二叉树的中序遍历,输出结果为:1 3 2。
阅读全文