遍历二叉树

#include <stdio.h>

//定义二叉树结构

typedef struct TreeNode

{

char val;

struct TreeNode* left;

struct TreeNode* right;

}TreeNode;

//创建前序二叉树

TreeNode* CreatTree(char* str,int *pi)

{

if(str[*pi]=='#')

{

(*pi)++;

return NULL;

}

else

{

TreeNode* root=(TreeNode*)malloc(sizeof(TreeNode));

root->val=str[*pi];

(*pi)++;

root->left=CreatTree(str,pi);

root->right=CreatTree(str,pi);

return root;

}

}

//打印中序二叉树

void InOrder(TreeNode* root)

{

if(root==NULL)

{

return;

}

InOrder(root->left);

printf("%c ",root->val);

InOrder(root->right);

}

int main() {

char str[100];

scanf("%s",str);//输入字符

int i=0;

TreeNode* root=CreatTree(str,&i);//把字符放入二叉树

InOrder(root);

return 0;

}

全部评论

相关推荐

05-16 11:16
已编辑
东华理工大学 Java
牛客73769814...:盲猜几十人小公司,庙小妖风大,咋不叫她去4️⃣呢😁
牛客创作赏金赛
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务