PAT1135(红黑书的判定)
There is a kind of balanced binary search tree named red-black tree in the data structure. It has the following 5 properties:
(1) Every node is either red or black.
(2) The root is black.
(3) Every leaf (NULL) is black.
(4) If a node is red, then both its children are black.
(5) For each node, all simple paths from the node to descendant leaves contain the same number of black nodes.
For example, the tree in Figure 1 is a red-black tree, while the ones in Figure 2 and 3 are not.
For each given binary search tree, you are supposed to tell if it is a legal red-black tree.
Input Specification:
Each input file contains several test cases. The first line gives a positive integer K (<=30) which is the total number of cases. For each case, the first line gives a positive integer N (<=30), the total number of nodes in the binary tree. The second line gives the preorder traversal sequence of the tree. While all the keys in a tree are positive integers, we use negative signs to represent red nodes. All the numbers in a line are separated by a space. The sample input cases correspond to the trees shown in Figure 1, 2 and 3.
Output Specification:
For each test case, print in a line "Yes" if the given tree is a red-black tree, or "No" if not.
Sample Input:
3 9 7 -2 1 5 -4 -11 8 14 -15 9 11 -2 1 -7 5 -4 8 14 -15 8 10 -7 5 -6 8 15 -11 17
Sample Output:
Yes No No
红黑树是一颗平衡的二叉树,但并不是完美的平衡二叉树,也就是并不满足左右节点的深度差的绝对值为1。解这道题的关键就是注意到作为一颗二叉搜索树,左节点的值是小于根节点,右节点的值大于根节点的。
有了这个性质,那么我们就可以根据先序遍历来构造出这棵树。
构造出树以后,根据红黑树的几个条件,我们进行判断就可以了。每个红色节点的左子树和右子树必须是黑色的,任意一个节点到其叶子节点的所有路径上黑色节点的数量是相同的。这两个条件都可以通过递归来实现。
同时,代码中给出了求后序序列的方法
#include<iostream> #include<cstdio> #include<vector> #include<cmath> using namespace std; struct Node { int val; Node *left,*right; }; vector<int>arr,pre,post; Node* build(Node *root ,int v) { if(root==NULL) { root =new Node(); root->val=v; root->left=root->right=NULL; } else if(abs(v)<=abs(root->val)) root->left=build(root->left,v); else root->right=build(root->right,v); return root; } void getpost(int root,int end) //获得后序遍历的结果 { if(root>end) return; int i=root+1,j=end; while(i<=end&&pre[root]>pre[i]) i++; while(j>=root+1&&pre[root]<=pre[j]) j--; if(j+1!=i) return; getpost(root+1,j);//左子树的根和重点 getpost(i,end); //右子树的根和终点 post.push_back(pre[root]); } bool judge1(Node *root) { if(root==NULL) return true; if(root->val<0) //红色节点 { if(root->left!=NULL&&root->left->val<0) return false; if(root->right!=NULL&&root->right->val<0) return false; } return judge1(root->left)&&judge1(root->right); //递归判断 } int getnum(Node *root) { if(root==NULL) return 0; int l=getnum(root->left); int r=getnum(root->right); return root->val>0 ? max(l,r)+1 : max(l,r); //本身就为黑色节点 } bool judge2(Node *root) { if(root==NULL) return true; int l=getnum(root->left); int r=getnum(root->right); if(l!=r) return false; return judge2(root->left)&&judge2(root->right);//递归进行判断 } int main() { int k,n; scanf("%d",&k); for(int i=1;i<=k;i++) { scanf("%d",&n); arr.resize(n+2); pre.resize(n+2); Node *root=NULL; for(int j=1;j<=n;j++) { scanf("%d",&arr[j]); root=build(root,arr[j]); pre[j]=abs(arr[j]); } post.clear(); getpost(1,n); if(arr[1]<0||judge1(root)==0||judge2(root)==0) printf("No\n"); else printf("Yes\n"); } }