浅沫记忆 发表于 2021-12-30 16:22:28

C++二叉树叶子结点的个数


//Author:PanDaoxi
#include <iostream>
using namespace std;
struct node{
int left,right;
char value;
}data;
int root=0,cnt=0;
char ch;
int buildTree(int bt){
cin>>ch;
if(ch=='#'){
    bt=0;
    return bt;
}
else{
    bt=++cnt;
    data.value=ch;
    data.left=data.right=0;
    data.left=buildTree(bt);
    data.right=buildTree(bt);
}
return bt;
}
int func(int bt){
if(bt){
    if(data.left==0&&data.right==0){
      return 1;
    }
    else {
      return func(data.left)+func(data.right);
    }
}
else return 0;
}
int main(){
root=0;
cnt=0;
root=buildTree(0);
cout<<func(root)<<endl;
return 0;
}







https://blog.51cto.com/u_15467636/4863015
页: [1]
查看完整版本: C++二叉树叶子结点的个数