题解 | #二叉树遍历#
二叉树遍历
https://www.nowcoder.com/practice/4b91205483694f449f94c179883c1fef
#include <stdio.h> #include <stdlib.h> #include <stdbool.h> #include <string.h> typedef struct tree { char string; struct tree* left; struct tree* right; }treeNode; treeNode* CreatTree(char* str,int* pi) { if(str[(*pi)]=='#') { (*pi)++; return NULL; } treeNode* root=(treeNode*)malloc(sizeof(treeNode)); root->string=str[(*pi)++]; root->left=CreatTree(str,pi); root->right=CreatTree(str,pi); return root; } void MinOrder(treeNode* root) { if(root==NULL) return; MinOrder(root->left); printf("%c ",root->string); MinOrder(root->right); } int main() { char string[100]; gets(string); int i=0; treeNode* root=CreatTree(string,&i); MinOrder(root); return 0; }