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
题目大意
要判断所给的BST是否符合下面性质: (1)每个节点或者是黑色,或者是红色。 (2)根节点是黑色。 (3)每个叶子节点(NIL)是黑色。 [注意:这里叶子节点,是指为空(NIL或NULL)的叶子节点!] (4)如果一个节点是红色的,则它的子节点必须是黑色的(从每个叶子到根的所有路径上不能有两个连续的红色节点) 。 (5)从任一节点到其每个叶子节点(NULL)的所有简单路径都包含相同数目的黑色节点。
思路
不太会做,只会建树和判断性质4,性质5不会判断,主要参考1135 Is It A Red-Black Tree(判断是否是红黑树)
#include <iostream>
#include <cstdlib>
#include <vector>
#include <algorithm>
using namespace std;
struct node
{
int val;
node* left;
node* right;
node(int v) : val(v), left(NULL), right(NULL) {}
};
int pre[31], in[31];
node* creat(int preL, int preR, int inL, int inR);
bool judge4(node* root);
bool judge5(node* root);
int cntBlack(node* root);
int main()
{
int K;
cin >> K;
for (int i = 0; i < K; i++)
{
int N;
cin >> N;
for (int j = 0; j < N; j++)
{
cin >> pre[j];
in[j] = abs(pre[j]);
}
sort(in, in + N - 1);
node* root = creat(0, N - 1, 0, N - 1);
if (pre[0] > 0 && judge4(root) && judge5(root))
cout << "Yes" << endl;
else
cout << "No" << endl;
}
system("pause");
return 0;
}
node* creat(int preL, int preR, int inL, int inR)
{
if (preL > preR)
return NULL;
node* root = new node(pre[preL]);
int k;
for (k = inL; k <= inR; k++)
if (in[k] == abs(pre[preL]))
break;
int numLeft = k - inL;
root->left = creat(preL + 1, preL + numLeft, inL, k - 1);
root->right = creat(preL + numLeft + 1, preR, k + 1, inR);
return root;
}
bool judge4(node* root)
{
if (!root)
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 judge4(root->left) && judge4(root->right);
}
bool judge5(node* root)
{
if (!root)
return true;
int leftBlackNum = cntBlack(root->left);
int rightBlackNum = cntBlack(root->right);
if (leftBlackNum != rightBlackNum)
return false;
return judge5(root->left) && judge5(root->right);
}
int cntBlack(node* root)
{
if (!root)
return 0;
int left = cntBlack(root->left);
int right = cntBlack(root->right);
if (root->val > 0)
return max(left, right) + 1;
else
return max(left, right);
}
|