题解 | #二叉树遍历#
二叉树遍历
https://www.nowcoder.com/practice/4b91205483694f449f94c179883c1fef
#include <stdio.h> #include<stdlib.h> typedef struct tnode { char data; struct tnode* left; struct tnode* right; } tnode; tnode* creat(char* s, int* i) { if (s[*i]) { if (s[*i] == '#') { return NULL; } else { tnode* newnode = (tnode*)malloc(sizeof(tnode)); newnode->data = s[*i]; (*i)++; newnode->left = creat(s, i); (*i)++; newnode->right = creat(s, i); return newnode; } } return NULL; } void inorder(tnode* root) { if (root == NULL) { return; } inorder(root->left); printf("%c ", root->data); inorder(root->right); } int main() { char s[100] = { 0 }; int i = 0; scanf("%s", s); tnode* root = creat(s, &i); inorder(root); }