首页 > 其他分享 >二叉树

二叉树

时间:2022-10-09 21:55:32浏览次数:43  
标签:node int pop front 二叉树 push

  • P4715淘汰赛
查看代码

/*
本题一眼就是二叉树,不过是满二叉树,把左右节点做比较,大的就做父节点,类似线段树的打法
不过这题也可以用队列+结构体实现,比对时大的继续留在队列,小的除去,符合FIFO原则,最后
专门去取最后俩个做对比就好了 
*/

include<bits/stdc++.h>

using namespace std;
int n;
struct node{
int id,w;
};
queue <node> q;
int main(){
scanf("%d",&n);
n=1<<n;
for(int i=1;i<=n;++i){
int w;
cin>>w;
q.push(node{i,w});
}
while(q.size()>2){
node a,b;
a=q.front();
q.pop();
b=q.front();
q.pop();
if(a.w>b.w) q.push(a);
else q.push(b);
}
node x,y;
x=q.front();
q.pop();
y=q.front();
q.pop();
if(x.w<y.w) cout<<x.id<<endl;
else cout<<y.id<<endl;
return 0;
}

标签:node,int,pop,front,二叉树,push
From: https://www.cnblogs.com/azdada34/p/16773822.html

相关文章