PTA Title Set data structures and algorithms (Chinese) 7-31

PTA Title Set data structures and algorithms (Chinese) 7-31

7-31 Cartesian tree (25 points)
 

Cartesian tree is a special binary tree, which node contains two keywords K1 and K2. First Cartesian tree is about K1 binary search tree, that node in the left subtree of all K1 value is less than the value of the node K1, right child is big. Second order all the nodes K2 keywords meet priority queue (may wish to set the minimum heap) requirements, i.e., the value of K2 is smaller than the node in its subtree K2 values ​​of all nodes. Given a binary tree, please determine whether the tree Cartesian tree.

Input formats:

Firstly, input a positive integer N ( ≤1000), the number of nodes in the tree. Then N rows, each node is given a message, comprising: K1 value of the node, K2 value, left child node numbers, right child node numbers. Provided from node 0 (N-1) order ~ number. If a node child node does not exist, the location is given -.

Output formats:

Output YESIf the tree is a Cartesian tree; otherwise the output NO.

Sample Input 1:

6
8 27 5 1
9 40 -1 -1
10 20 0 3
12 21 -1 4
15 22 -1 -1
5 35 -1 -1

Output Sample 1:

YES

Sample Input 2:

6
8 27 5 1 9 40 -1 -1 10 20 0 3 12 11 -1 4 15 22 -1 -1 50 35 -1 -1 

Output Sample 2:

NO
Topic analysis: a tree mainly on word problems is to pay attention not only to meet each sub-tree is smaller than the left than the overall large tree on the right when we need to meet the definition and understanding of the balanced binary tree priority queue (minimum heap) definition of balanced binary judgment the concept of balanced binary tree
  1 #define _CRT_SECURE_NO_WARNINGS
  2 #include<stdio.h>
  3 #include<string.h>
  4 #include<malloc.h>
  5 
  6 struct TreeNode
  7 {
  8     int K1;
  9     int K2;
 10     int Lc;
 11     int Rc;
 12 }Tr[1000];
 13 
 14 int Collected[1000];
 15 
 16 int FindTree(int N)
 17 {
 18     for (int i = 0; i < N; i++)
 19         if (!Collected[i])
 20             return i;
 21 }
 22 
 23 
 24 
 25 int IsAVL(int Tree)
 26 {
 27     if (Tree ==-1)
 28         return 1;
 29     else
 30     {
 31         if (Tr[Tree].Lc != -1 && Tr[Tree].Rc != -1)
 32             if (Tr[Tree].K1 >=Tr[Tr[Tree].Lc].K1 && Tr[Tree].K1 < Tr[Tr[Tree].Rc].K1)
 33                 return IsAVL(Tr[Tree].Lc) && IsAVL(Tr[Tree].Rc);
 34             else
 35                 return 0;
 36         else if (Tr[Tree].Lc == -1 && Tr[Tree].Rc == -1)
 37             return 1;
 38         else if (Tr[Tree].Lc == -1)
 39             return Tr[Tree].K1 < Tr[Tr[Tree].Rc].K1;
 40         else
 41             return Tr[Tree].K1 >=Tr[Tr[Tree].Lc].K1;
 42 
 43     }
 44 }
 45 
 46 int IsMinHeap(int Tree)
 47 {
 48     if (Tree ==-1)
 49         return 1;
 50     else
 51     {
 52         if (Tr[Tree].Lc != -1 && Tr[Tree].Rc != -1)
 53             if (Tr[Tree].K2 <=Tr[Tr[Tree].Lc].K2 && Tr[Tree].K2 <=Tr[Tr[Tree].Rc].K2)
 54                 return IsMinHeap(Tr[Tree].Lc) && IsMinHeap(Tr[Tree].Rc);
 55             else
 56                 return 0;
 57         else if (Tr[Tree].Lc == -1 && Tr[Tree].Rc == -1)
 58             return 1;
 59         else if (Tr[Tree].Lc == -1)
 60             return Tr[Tree].K2 <=Tr[Tr[Tree].Rc].K2;
 61         else
 62             return Tr[Tree].K2 <=Tr[Tr[Tree].Lc].K2;
 63     }
 64 }
 65 int JudgetLeft(int Tree, int T);
 66 int JudgetRight(int Tree, int T);
 67 
 68 int JudgetLeft(int Tree,int T)
 69 {
 70     if (T == -1 || Tree == -1)
 71         return 1;
 72     if (Tr[Tree].K1 > Tr[T].K1)
 73         return JudgetLeft(Tree, Tr[T].Lc) && JudgetLeft(Tree,Tr[T].Rc);
 74     else
 75         return 0;
 76 }
 77 
 78 int JudgetRight(int Tree, int T)
 79 {
 80     if (T == -1 || Tree == -1)
 81         return 1;
 82     if (Tr[Tree].K1 < Tr[T].K1)
 83         return JudgetRight(Tree, Tr[T].Lc) && JudgetRight(Tree, Tr[T].Rc);
 84     else
 85         return 0;
 86 }
 87 
 88 int IsTree(int Tree)
 89 {
 90     if (Tree == -1)
 91         return 1;
 92     if(JudgetLeft(Tree,Tr[Tree].Lc)&&JudgetRight(Tree,Tr[Tree].Rc))
 93         return IsTree(Tr[Tree].Lc)&&IsTree(Tr[Tree].Rc);
 94     else
 95         return 0;
 96 }
 97 
 98 int main()
 99 {
100     int N;
101     scanf("%d", &N);
102     for (int i = 0; i < N; i++)
103     {
104         int K1, K2, Lc, Rc;
105         scanf("%d%d%d%d", &K1, &K2, &Lc, &Rc);
106         Tr[i].K1 = K1;
107         Tr[i].K2 = K2;
108         Tr[i].Lc = Lc;
109         Tr[i].Rc = Rc;
110         if (Lc != -1)
111             Collected[Lc] = 1;
112         if (Rc != -1)
113             Collected[Rc] = 1;
114     }
115     int Tree = FindTree(N);
116     if (IsAVL(Tree) && IsMinHeap(Tree)&&IsTree(Tree))
117         printf("YES");
118     else
119         printf("NO");
120     return 0;
121 }
View Code

 

Guess you like

Origin www.cnblogs.com/57one/p/11648998.html