题解 | #二叉树遍历#
二叉树遍历
https://www.nowcoder.com/practice/4b91205483694f449f94c179883c1fef
#include <stdio.h> #include <stdlib.h> typedef char BTDataType; typedef struct BinaryNode { BTDataType data; struct BinaryNode* left; struct BinaryNode* right; }BTNode; BTNode* BuyNode(BTDataType val) { BTNode* newNode = (BTNode*)malloc(sizeof(BTNode)); if(newNode == NULL) { perror("malloc fail"); return NULL; } newNode->data=val; newNode->left=NULL; newNode->right=NULL; return newNode; } BTNode* OrderBinaryCreateHelper(BTDataType* a,int* pi) { if(a[*pi] == '#') { (*pi)++; return NULL; } BTNode* newNode = BuyNode(a[(*pi)++]); newNode->left = OrderBinaryCreateHelper(a, pi); newNode->right=OrderBinaryCreateHelper(a, pi); return newNode; } void BTreeQrder(BTNode* root) { if(root ==NULL) return ; BTreeQrder(root->left); printf("%c ",root->data); BTreeQrder(root->right); } int main() { char a[101]; scanf("%s",a); int Index = 0; BTNode* root = OrderBinaryCreateHelper(a, &Index); BTreeQrder(root); return 0; }