练习题:判断完全二叉树

题面:

  练习题:判断完全二叉树

思路:

  还要说吗.

代码:

  

练习题:判断完全二叉树
 1 #include <iostream>
 2 #include <stdlib.h>
 3 
 4 using namespace std;
 5 
 6 int n,a[10010];
 7 
 8 struct tree
 9 {
10     int s;
11     tree *l,*r;
12 };
13 
14 tree* made()
15 {
16     tree *p;
17     p=new tree;
18     int name;
19     cin>>name;
20     if(name==-1)
21     {
22         p=NULL;
23     }
24     else
25     {
26         p->s=name;
27         p->l=made();
28         p->r=made();
29     }
30     return p;
31 }
32 
33 void out(tree *p)
34 {
35     if(p==NULL)
36     {
37         return ;
38     }
39     if(p->l!=NULL)
40     {
41         if(p->l->s!=p->s*2)
42         {
43             cout<<"NO"<<endl;
44             exit(0);
45         }
46     }
47     if(p->r!=NULL)
48     {
49         if(p->r->s!=p->s*2+1)
50         {
51             cout<<"NO"<<endl;
52             exit(0);
53         }
54     }
55     out(p->l);
56     out(p->r);
57 }
58 
59 int main()
60 {
61     tree *p=new tree;
62     p=made();
63     out(p);
64     cout<<"YES"<<endl;
65     return 0;
66 }
Code

 

上一篇:pytest文档46-关于https请求警告问题(InsecureRequestWarning: Unverified HTTPS request is being made)


下一篇:【英语学习】【Level 07】U03 Amazing wonders L5 Modern structures in China