题解 | #二叉树遍历#
二叉树遍历
https://www.nowcoder.com/practice/4b91205483694f449f94c179883c1fef
#include <stdio.h> #include <string.h> #include <stdlib.h> typedef struct Tnode { char data; struct Tnode* lchild; struct Tnode* rchild; } Tnode; void midorder(Tnode* root) { if (root != NULL) { midorder(root->lchild); printf("%c ", root->data); midorder(root->rchild); } else { return ; } } int n = 0; Tnode* maketree(char* x) { static int n = 0; if (x[n] == '#') { n++; // printf("%d",n); return NULL; } Tnode* t = (Tnode*)calloc(1, sizeof(Tnode)); t->data = x[n]; n++; // printf("%d",n); t->lchild = maketree(x); t->rchild = maketree(x); return t; } int main() { char x[101]; while (scanf("%s", &x) != EOF) { Tnode* root; root = maketree(x); midorder(root); } }