zoukankan      html  css  js  c++  java
  • NOJ1019-计算二叉树的高度和结点数

    输入

     

    二叉树的先序遍历序列,用#代表空树或空子树。

    输出

     

    共五行

    前三行依次输出先序、中序和后序遍历序列,

    第四行输出二叉树的高度,

    第五行依次输出二叉树总结点数目、叶子结点数目、度为1的结点数目。

    样例输入

    A B # D # # C E # # F # #

    样例输出

    PreOrder: A B D C E F
    InOrder: B D A E C F
    PostOrder: D B E F C A
    3
    6 3 1

    题目很简单,基本的二叉树操作。需要注意的是输入结点之间有空格,而且输出结点时也有空格,行末不能有空格。

      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 int node_count = 0;
     12 int leaf_count = 0;
     13 int one_count = 0;
     14 
     15 void Create( BTNode*& t) {
     16     char c;
     17     char ch;
     18     scanf( "%c", &c );
     19     ch = getchar();
     20     if( c =='#' )
     21         t = NULL;
     22     else {
     23         t = new BTNode;
     24         t->data = c;
     25         Create( t->left_child );
     26         Create( t->right_child );
     27     }
     28 }
     29 
     30 void PreOrder( BTNode* t ) {
     31     if( t != NULL ) {
     32         printf( " %c", t->data );
     33         PreOrder( t->left_child );
     34         PreOrder( t->right_child );
     35     }
     36 }
     37 
     38 void InOrder( BTNode *t ) {
     39     if( t != NULL ) {
     40         InOrder( t->left_child );
     41         printf( " %c", t->data );
     42         InOrder( t->right_child );
     43     }
     44 }
     45 
     46 void PostOrder( BTNode *t ) {
     47     if( t != NULL ) {
     48         PostOrder( t->left_child );
     49         PostOrder( t->right_child );
     50         printf( " %c", t->data );
     51     }
     52 }
     53 
     54 int Height( BTNode *t ) {
     55     int i, j;
     56     if( t == NULL ) return 0;
     57     else {
     58          i = Height( t->left_child );
     59          j = Height( t->right_child );
     60     }
     61     return ( i > j ) ? ( i + 1 ) : ( j + 1 );
     62 }
     63 
     64 void BTNode_Count( BTNode *t ) {
     65     if( t == NULL ) return ;
     66     else {
     67          BTNode_Count( t->left_child );
     68          BTNode_Count( t->right_child );
     69          node_count++;
     70     }
     71 }
     72 
     73 void BTNode_LeafCount( BTNode *t ) {
     74     if( t == NULL ) return ;
     75     else {
     76         if( t->left_child == NULL && t->right_child == NULL ) {
     77             leaf_count++;
     78         }
     79         else if( t->left_child == NULL && t->right_child != NULL || t->left_child != NULL && t->right_child == NULL ){
     80             one_count++;
     81         }
     82         BTNode_LeafCount( t->left_child );
     83         BTNode_LeafCount( t->right_child );
     84     }
     85 }
     86 
     87 int main() {
     88     BTNode T;
     89     BinTree root = &T;
     90     Create( root );
     91     printf( "PreOrder:" );
     92     PreOrder( root );
     93     printf( "
    " );
     94     printf( "InOrder:" );
     95     InOrder( root );
     96     printf( "
    " );
     97     printf( "PostOrder:" );
     98     PostOrder( root );
     99     int height = Height( root );
    100     BTNode_Count( root );
    101     BTNode_LeafCount( root );
    102     printf( "
    %d
    %d %d %d", height, node_count, leaf_count, one_count );
    103     return 0;
    104 }
  • 相关阅读:
    ie 标题写入问题
    预览图片
    form表单submit事件
    SpringGraph 20101030 11:08[转]
    flexunable to transcode image
    navicate导入和导出数据库(转)
    Flex转自新浪微博
    zk中的Datebox中得到Timestamp
    反向代理
    flash builder sdk 4[转自IYeye]
  • 原文地址:https://www.cnblogs.com/lzjtdxfxl/p/5380474.html
Copyright © 2011-2022 走看看