输入
输入为接受键盘输入的由大写英文字符和"#"字符构成的一个字符串(用于创建对应的二叉树)。
输出
输出该用例对应的二叉树中的空链域个数。
样例输入复制
A## ABC#### AB##C## ABCD###EF##G### A##B##
样例输出复制
2 4 4 8 2
#include <iostream>
#include <cstdlib>
using namespace std;
typedef struct BitNode{
char data;
struct BitNode *lc,*rc;
}BitNode,*BitTree;
void CreateBitTree(BitTree &T){
char ch; cin >> ch;
if(ch=='#')
T = NULL;
else{
T = (BitTree)malloc(sizeof(BitNode));
T->data = ch;
CreateBitTree(T->lc);
CreateBitTree(T->rc);
}
}
int n=0;
int VisitBitTree(BitTree T){
if(T!=NULL){
VisitBitTree(T->lc);
VisitBitTree(T->rc);
}else n++; //统计空链
return n;
}
int main(){
BitTree T = NULL;
// T = (BitTree)malloc(sizeof(BitNode));
CreateBitTree(T);
cout << VisitBitTree(T);
return 0;
}