二叉树叶子节点的个数和第k层的节点个数——题集八

二叉树叶子节点的个数和第k层的节点个数——题集八

       今天,博主没懒丢,所以来分享一下二叉树叶子节点的个数第k层的节点个数求数组中数字次数超过数组一半的字符

       求二叉树叶子节点的个数的源程序和运行用例。

源代码如下:

#include<iostream>
using namespace std;
#include<stack>
 
struct TreeNode {
    int val;
    struct TreeNode *left;
    struct TreeNode *right;
    TreeNode(int x)
:val(x)
,left(NULL)
,right(NULL)
{
    }
};
 
//二叉树叶子节点的个数
int Leafnum(TreeNode* root){//二叉树叶子节点的个数
//前序遍历——循环
int num=0;
if(root == NULL)return num;
stack<TreeNode*> tmp;
tmp.push(root);
TreeNode* cur;
 
while(!tmp.empty()){
cur=tmp.top();
 
while(cur->left  != NULL){
cur=cur->left;
tmp.push(cur);
}
 
tmp.pop();
while(cur->right == NULL){
if(cur->left  == NULL){
num++;
}
if(tmp.empty()) return num;
cur=tmp.top();
tmp.pop();
}
cur=cur->right;
tmp.push(cur);
}
return num;
}
 
void Testtreeleft(){//二叉树叶子节点的个数
TreeNode pRoot1(1);
TreeNode pRoot2(2);
TreeNode pRoot3(3);
TreeNode pRoot4(4);
TreeNode pRoot5(5);
TreeNode pRoot6(6);
TreeNode pRoot7(7);
TreeNode pRoot8(8);
pRoot1.left = &pRoot2;
pRoot1.right = &pRoot3;
pRoot2.left = &pRoot4;
pRoot2.right = &pRoot5;
pRoot3.left = &pRoot6;
pRoot3.right = &pRoot7;
pRoot4.left = &pRoot8;
 
cout<<"二叉树叶子节点的个数"<<endl;
cout<<"Leafnum(&pRoot1): "<<Leafnum(&pRoot1)<<endl;//二叉树叶子节点的个数
cout<<"Leafnum(&pRoot2): "<<Leafnum(&pRoot2)<<endl;
cout<<"Leafnum(&pRoot3): "<<Leafnum(&pRoot3)<<endl;
cout<<"Leafnum(&pRoot4): "<<Leafnum(&pRoot4)<<endl;
cout<<"Leafnum(&pRoot5): "<<Leafnum(&pRoot5)<<endl;
}
 
int main(){
Testtreeleft();//二叉树叶子节点的个数
system("pause");
return 0;
}

运行结果:

 

       求二叉树第k层的节点个数的源程序和运行用例。

源代码如下:

#include<iostream>
using namespace std;
#include<queue>
 
struct TreeNode {
    int val;
    struct TreeNode *left;
    struct TreeNode *right;
    TreeNode(int x)
:val(x)
,left(NULL)
,right(NULL)
{
    }
};
 
//二叉树第k层的节点个数
int NodeKnum(TreeNode* root, int k){
int num=0;
if(k<=0 || root==NULL) return num;//层序遍历
++num;
if(k==1) return num;
queue<TreeNode*> tmp;
tmp.push(root);
int i=1;//层序
TreeNode* cur;
 
while(!tmp.empty()){
int count=0;
while(num-- >0){
cur=tmp.front();
tmp.pop();
 
if(cur->left != NULL){
tmp.push(cur->left);
++count;
}
if(cur->right != NULL){
++count;
tmp.push(cur->right);
}
}
num=count;
i++;
if(i==k) return num;
}
return 0;
}
 
void TesttreeKnum(){//二叉树第k层的节点个数
TreeNode pRoot1(1);
TreeNode pRoot2(2);
TreeNode pRoot3(3);
TreeNode pRoot4(4);
TreeNode pRoot5(5);
TreeNode pRoot6(6);
TreeNode pRoot7(7);
TreeNode pRoot8(8);
pRoot1.left = &pRoot2;
pRoot1.right = &pRoot3;
pRoot2.left = &pRoot4;
pRoot2.right = &pRoot5;
pRoot3.left = &pRoot6;
pRoot3.right = &pRoot7;
pRoot4.left = &pRoot8;
 
cout<<"二叉树第k层的节点个数"<<endl;
cout<<"NodeKnum(&pRoot1, 4): "<<NodeKnum(&pRoot1, 4)<<endl;//二叉树第k层的节点个数
cout<<"NodeKnum(&pRoot1, 3): "<<NodeKnum(&pRoot1, 3)<<endl;
cout<<"NodeKnum(&pRoot1, 2): "<<NodeKnum(&pRoot1, 2)<<endl;
cout<<"NodeKnum(&pRoot1, 1): "<<NodeKnum(&pRoot1, 1)<<endl;
cout<<"NodeKnum(&pRoot1, 0): "<<NodeKnum(&pRoot1, 0)<<endl;
}
 
int main(){
TesttreeKnum();//二叉树第k层的节点个数
system("pause");
return 0;
}

运行结果:

 

       一个数组中有一个数字的次数超过了数组的一半,求出这个字符,不存在输出*。

       如:int a[]={2,3,2,2,2,2,2,5,4,1,2,3},求出超过一半的数字是2。

       求数组中数字次数超过数组一半的字符的源程序和运行用例。

源代码如下:

#include<iostream>
using namespace std;
#include<map>
 
//求数组中数字次数超过数组一半的字符,不存在输出*。
char Halfnum(vector<char>* num){
char ch('*');
map<char, int> tmp;
int size=num->size();
 
for(int i=0; i<size; i++){
tmp[(*num)[i]]++;
}
 
map<char, int>::iterator it=tmp.begin();
while(it != tmp.end()){
if(it->second >(size/2)){
ch=it->first;
break;
}
++it;
}
return ch;
}
 
void printnum(vector<char>* num){
int size=num->size();
 
for(int i=0; i<size; i++){
cout<<(*num)[i]<<" ";
}
cout<<endl;
}
 
void TestHalfnum(){//求数组中数字次数超过数组一半的字符
vector<char> num;
num.push_back('2');
num.push_back('3');
num.push_back('2');
num.push_back('2');
num.push_back('5');
num.push_back('2');
 
cout<<"数组为:";
printnum(&num);
cout<<"求数组中数字次数超过数组一半的字符"<<endl;
cout<<"Halfnum(num): "<<Halfnum(&num)<<endl<<endl;//求数组中数字次数超过数组一半的字符,不存在输出*。
 
num.pop_back();
cout<<"数组为:";
printnum(&num);
cout<<"求数组中数字次数超过数组一半的字符"<<endl;
cout<<"Halfnum(num): "<<Halfnum(&num)<<endl<<endl;//求数组中数字次数超过数组一半的字符,不存在输出*。
 
num.pop_back();
num.pop_back();
cout<<"数组为:";
printnum(&num);
cout<<"求数组中数字次数超过数组一半的字符"<<endl;
cout<<"Halfnum(num): "<<Halfnum(&num)<<endl<<endl;//求数组中数字次数超过数组一半的字符,不存在输出*。
}
 
int main(){
TestHalfnum();//求数组中数字次数超过数组一半的字符
system("pause");
return 0;
}

运行结果:

 

     分享如上,如有错误,望斧正!愿大家学得开心,共同进步!

ps:上一篇昨天就写完,我为啥不发。。。

     懒~^v^,希望大家以我为鉴,不要,懒的,不,发,博客。T_T

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值