https://blog.csdn.net/wanglinyp/article/details/104967970?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-5.channel_param&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-5.channel_param
https://blog.csdn.net/sinat_41144773/article/details/89530403
https://blog.csdn.net/wanglinyp/article/details/104967970?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-5.channel_param&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-5.channel_param
//30
// 8
// 38 24 12 45 58 67 42 51
// 38 45 24 58 42 12 67 51
// NO
// 9
// 38 45 42 24 58 30 67 12 51
// 38 45 24 58 42 30 12 67 51
// YES
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
using namespace std;
struct node{
int data;
node* lchild;
node* rchild;
};
int n;
int in[110];
void insert(node* &root,int data){
if(root==NULL){
root=new node;
root->data=data;
root->lchild=root->rchild=NULL;
return;
}
if(data>root->data)insert(root->lchild,data);
else insert(root->rchild,data);
}
void bfs(node* root){
queue<node*> q;
q.push(root);
bool flag=true;
while(!q.empty()){
node* now=q.front();
q.pop();
if(flag==true){
cout<<now->data;
flag=false;
}
else
cout<<" "<<now->data;
if(now->lchild!=NULL)q.push(now->lchild);
if(now->rchild!=NULL)q.push(now->rchild);
}
cout<<endl;
}
bool isCBT(node* root){
queue<node*> q;
q.push(root);
int num=0;
while(q.front()!=NULL){
node* now=q.front();
q.pop();
q.push(now->lchild);
q.push(now->rchild);
num++;
}
if(num==n)return true;
else return false;
}
int main()
{
cin>>n;
node* root=NULL;
for(int i=0;i<n;i++){
int data;
cin>>data;
insert(root,data);
}
bfs(root);
bool f=isCBT(root);
if(f==true)cout<<"YES";
else cout<<"NO";
return 0;
}
-----
#include <iostream>
#include <cstdio>
#include <queue>
#include <vector>
using namespace std;
//vector<int> layerV;
int n;
struct node{
int data;
node* lchild;
node* rchild;
};
void insert(node* &root,int data){
if(root==NULL){
root=new node();
root->data=data;
root->lchild=root->rchild=NULL;
return;
}
if(data>root->data)insert(root->lchild,data);
else insert(root->rchild,data);
}
void layerOrder(node* root){
queue<node*>q;
q.push(root);
bool flag=true;
while(!q.empty()){
node* top = q.front();
q.pop();
//layerV.push_back(top->data);
if(flag==true){
cout<<top->data;
flag=false;
}else{
cout<<" "<<top->data;
}
if(top->lchild!=NULL)q.push(top->lchild);
if(top->rchild!=NULL)q.push(top->rchild);
}
}
bool judge(node* root){
if(root==NULL)return true;
queue<node*>q;
q.push(root);
int sum=0;
while(q.front()!=NULL){
node* top = q.front();
q.pop();
//v.push_back(top->data);
q.push(top->lchild);
q.push(top->rchild);
sum++;
}
if(sum==n)return true;
else return false;
}
int main(){
cin>>n;
node* root=NULL;
for(int i=0;i<n;i++){
int tmp;
cin>>tmp;
insert(root,tmp);
}
layerOrder(root);
// for(int i=0;i<layerV.size();i++){
// if(i==0){
// cout<<layerV[i];
// }else{
// cout<<" "<<layerV[i];
// }
// }
cout<<endl;
bool flag=judge(root);
if(flag==true){
cout<<"YES";
}else{
cout<<"NO";
}
return 0;
}
今天的文章完全二叉树判断代码_二叉树遍历过程看不懂「建议收藏」分享到此就结束了,感谢您的阅读。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/85941.html