1066. Root of AVL Tree
来源:互联网 发布:js给div添加内容 编辑:程序博客网 时间:2024/05/21 04:16
#include<algorithm>#include<iostream>using namespace std;struct node{ node*lch,*rch; int key; node(int x):key(x){lch=rch=nullptr;}};using ptr=node*;int deep(ptr p){ if(!p)return 0; return max(deep(p->lch),deep(p->rch))+1;}ptr ll(ptr p){//右旋 ptr l=p->lch; p->lch=l->rch,l->rch=p; return l;}ptr rr(ptr p){//左旋 ptr r=p->rch; p->rch=r->lch,r->lch=p; return r;}ptr rl(ptr p){ p->rch=ll(p->rch); return rr(p);}ptr lr(ptr p){ p->lch=rr(p->lch); return ll(p);}ptr insert(ptr p,int x){ if(!p)return new node(x); if(x<p->key) { p->lch=insert(p->lch,x); if(deep(p->lch)-deep(p->rch)==2) return x<p->lch->key?ll(p):lr(p); }else { p->rch=insert(p->rch,x); if(deep(p->rch)-deep(p->lch)==2) return x<p->rch->key?rl(p):rr(p); } return p;}int main(){ int n;cin>>n; ptr p=nullptr; for(int t,i=0;i<n;++i) { cin>>t; p=insert(p,t); } cout<<p->key;}
0 0
- 1066. Root of AVL Tree
- 1066. Root of AVL Tree
- 1066.Root of AVL Tree
- 1066. Root of AVL Tree
- 1066. Root of AVL Tree
- 1066. Root of AVL Tree
- Root of AVL Tree
- Root of AVL Tree
- Root of AVL Tree
- 【PAT】1066. Root of AVL Tree (25)
- 【PAT】1066. Root of AVL Tree (25)
- 1066. Root of AVL Tree (25)
- PAT 1066. Root of AVL Tree
- PAT 1066. Root of AVL Tree
- 1066. Root of AVL Tree (25)
- 1066. Root of AVL Tree (25)
- 1066. Root of AVL Tree (25)
- 1066. Root of AVL Tree (25)
- Cocos2d-x 之AndroidManifest.xml 解释
- iptables启动脚本分析
- java 异常分类
- uva 133 The Dole Queue(循环队列)
- 为什么时间很紧还想玩游戏?
- 1066. Root of AVL Tree
- C++ 获取文件夹下的所有文件名
- hdu 1203 I NEED A OFFER!
- ElasticSearch安装ik分词插件
- LeetCode OJ算法题(二十一):Generate Parentheses
- 解决重启iptables内核模块自动unload
- Python 模块结构和布局
- SublimeText 视频教程
- 进程、线程和协程的理解