The given code has several issues:

  1. In the NodeCount function, the base case is checking if the tree is NULL, but the function returns 0 instead of 1. This is incorrect as a single node tree should have a count of 1. The correct statement should be if(T==NULL) return 1;
  2. The function is returning T->lchild+T->rchild which is not a valid operation as T->lchild and T->rchild are pointers to nodes, not integers. The correct statement should be return NodeCount(T->lchild) + NodeCount(T->rchild);
  3. The main function is using printf instead of cout to print the result. This is not an issue, but it is not consistent with the rest of the code which uses cin and cout.

Here is the corrected code:

#include<iostream>
using namespace std;
typedef struct BiNode{                
    char data;
    struct BiNode *lchild,*rchild;
}BiTNode,*BiTree;

void CreateBiTree(BiTree &T){    
    char ch;
    cin >> ch;
    if(ch=='#')  T=NULL;            
    else{                            
        T=new BiTNode;
        T->data=ch;                
        CreateBiTree(T->lchild);
        CreateBiTree(T->rchild);
    }
}

int NodeCount ( BiTree T)
{ 
    if(T==NULL) return 1;
    if(T->lchild==NULL&&T->rchild!=NULL)
        return 1;
    if(T->lchild!=NULL&&T->rchild==NULL)
        return 1;
    return NodeCount(T->lchild) + NodeCount(T->rchild);
}

int main(){
    BiTree T;
    CreateBiTree(T);
    cout << NodeCount(T);
    return 0;
}
``
#includeiostreamusing namespace std;typedef struct BiNode char data; struct BiNode lchildrchild;BiTNodeBiTree;void CreateBiTreeBiTree &T char ch; cin ch; ifch==# T

原文地址: https://www.cveoy.top/t/topic/fkhU 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录