题解 | #二叉树遍历#
二叉树遍历
http://www.nowcoder.com/practice/4b91205483694f449f94c179883c1fef
#include<stdio.h> #include<stdlib.h> typedef struct TreeNode{ struct TreeNodeleft; struct TreeNoderight; char data; }TreeNode;
TreeNode* TreeCreate(chara,inti) { if(a[*i]=='#') { (*i)++; return NULL; }
TreeNode*root=(TreeNode*)malloc(sizeof(TreeNode*));
root->data=a[(*i)++];
root->left=TreeCreate(a,i);
root->right=TreeCreate(a,i);
return root;
}
void InOrder(TreeNoderoot) { if(root==NULL) { return; } InOrder(root->left); printf("%c ",root->data); InOrder(root->right); } int main() { char a[100]; scanf("%s",a); //创建二叉树 int i=0; TreeNoderoot=TreeCreate(a,&i); InOrder(root); }