5-31笛卡尔树(25 分)
2016-04-24 16:03
393 查看
#include <iostream> #include "queue" #include "deque" #include "algorithm" #include "string.h" using namespace std; #define Max 100 struct Le { int k1,k2; int left,right; }; Le A[Max]; int Examine[Max]; struct Node { int K1; int K2; Node *Left; Node *Right; }; deque<int>Q1,Q2; class Tree { private: Node *root; public: Tree(){root=NULL;} Node*& Root(){return root;} void Output(Node *p); void Output(); void JudgeK1(Node *p); void JudgeK1(); bool JudgeK2(Node *p); bool JudgeK2(); }; bool Tree::JudgeK2() { return JudgeK2(root); } bool Tree::JudgeK2(Node *p) { if(p==NULL) return true; if(p->Left==NULL && p->Right==NULL) return true; if(p->Left!=NULL && p->Right==NULL) { if(p->K2<p->Left->K2) return true; return false; } if(p->Left==NULL && p->Right!=NULL) { if(p->K2<p->Right->K2) return true; return false; } bool T1,T2; T1=JudgeK2(p->Left); T2=JudgeK2(p->Right); if(T1 && T2) return true; return false; } void Tree::JudgeK1() { JudgeK1(root); } void Tree::JudgeK1(Node *p) { if(p!=NULL) { JudgeK1(p->Left); Q1.push_back(p->K1); Q2.push_back(p->K1); JudgeK1(p->Right); } } void Build(Node *&p,int i) { p=new Node; p->K1=A[i].k1; p->K2=A[i].k2; if(A[i].left!=-1) { Build(p->Left,A[i].left); } else p->Left=NULL; if(A[i].right!=-1) { Build(p->Right,A[i].right); } else p->Right=NULL; } int main() { memset(Examine,0,sizeof(Examine)); int n,i; cin>>n; for(i=0;i<n;i++) { cin>>A[i].k1>>A[i].k2>>A[i].left>>A[i].right; if(A[i].left!=-1) Examine[A[i].left]=1; if(A[i].right!=-1) Examine[A[i].right]=1; } int root=0; while(Examine[root]==1) root++; Tree tree; Build(tree.Root(),root); bool flag=true; tree.JudgeK1(); sort(Q2.begin(),Q2.end()); for(i=0;i<Q1.size();i++) { if(Q1[i]>Q2[i]) flag=false; } if(flag==false) { cout<<"NO"<<endl; } else { if(tree.JudgeK2()) cout<<"YES"<<endl; else cout<<"NO"<<endl; } return 0; }
相关文章推荐
- 今天第几天
- Netty学习之分隔符解决TCP粘包
- 第一次注册csdn博客
- Servlet监听器
- sql 、linq、lambda 查询语句的区别
- 117. Populating Next Right Pointers in Each Node II
- hadoop程序开发之——单机配置
- 【UE4学习】07——设置自己的模板 Converting a Project to a Template
- 不完全node实践教程-第五发
- 概率论部分整理
- linux下部署kodexplorer
- Adaboost 算法的原理与推导
- 搭建notepad++ python环境, 配置numpy 与 matplotlib
- nginx编译安装
- 禁止在 .NET Framework 中执行用户代码。启用 "clr enabled" 配置选项
- 压力测试 rsync+sersync
- 第七周上机实践项目 项目2-友元类
- 利用io流 书写简单的本地存储学生的学生管理code
- 【c/c++】bitset
- 数据库SQL语句练习题