NOJ1018-深度遍历二叉树

题目要求很简单,前中后序遍历一棵二叉树。坑爹的是这道题的输入数据和测试数据压根不一样,找了好久原因,去讨论区看见有别人发的测试样例,修改了一下就AC了

测试样例是这个:DEH##FJ##G#CK###A#B##

 1 #include <cstdio>
 2 
 3 typedef char TElemType;
 4 
 5 typedef struct node {
 6     TElemType data;
 7     struct node *left_child;
 8     struct node *right_child;
 9 } BTNode, *BinTree;
10 
11 //TElemType ch[] = { 'A', 'B', '#', 'D', '#', '#', 'C', 'E', '#', '#', 'F', '#', '#' };
12 //DEH##FJ##G#CK###A#B##
13 TElemType ch[] = { 'D', 'E', 'H', '#', '#', 'F', 'J', '#', '#', 'G', '#', 'C', 'K', '#', '#', '#', 'A', '#', 'B', '#', '#' };
14 int count = 0;
15 
16 void Create( BTNode*& t ) {
17     char c;
18     c = ch[count++];
19     if( c =='#' )
20         t = NULL;
21     else {
22         t = new BTNode;
23         t->data = c;
24         Create( t->left_child );
25         Create( t->right_child );
26     }
27 }
28 
29 void PreOrder( BTNode* t ) {
30     if( t != NULL ) {
31         printf( " %c", t->data );
32         PreOrder( t->left_child );
33         PreOrder( t->right_child );
34     }
35 }
36 
37 void InOrder( BTNode *t ) {
38     if( t != NULL ) {
39         InOrder( t->left_child );
40         printf( " %c", t->data );
41         InOrder( t->right_child );
42     }
43 }
44 
45 void PostOrder( BTNode *t ) {
46     if( t != NULL ) {
47         PostOrder( t->left_child );
48         PostOrder( t->right_child );
49         printf( " %c", t->data );
50     }
51 }
52 
53 int main() {
54     BTNode T;
55     BinTree root = &T;
56     Create( root );
57     printf( "PreOrder:" );
58     PreOrder( root );
59     printf( "
" );
60     printf( "InOrder:" );
61     InOrder( root );
62     printf( "
" );
63     printf( "PostOrder:" );
64     PostOrder( root );
65     return 0;
66 }
原文地址:https://www.cnblogs.com/lzjtdxfxl/p/5380488.html