您的位置:首页 > 其它

PAT (Advanced Level)1135. Is It A Red-Black Tree (30) 指针 建树 深度优先遍历

2018-03-09 14:54 751 查看

1135. Is It A Red-Black Tree (30)

时间限制400 ms
内存限制65536 kB
代码长度限制16000 B
判题程序Standard作者CHEN, Yue
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.



Figure 1Figure 2Figure 3
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 in
4000
teger 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

代码来源//1135. Is It A Red - Black Tree(30)

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;

int n, a[100];

struct Node
{
int val;//节点
int bBlack;//是黑树吗?
Node* left;
Node* right;

void setVal(int iVal)
{
if (iVal > 0) bBlack = 1;//节点为黑?
else if (iVal < 0)bBlack = 0;//节点为红?
val = abs(iVal);
}
};

Node* CreateTree(int l, int r)//建树(背下来)
{
if (l > r) return NULL;
Node* nd = new Node();//指向Node类型的指针
nd->setVal(a[l]);
int i = l + 1;
for (; i <= r; ++i)
if (abs(a[i]) > abs(a[l])) break;
nd->left = CreateTree(l + 1, i - 1);//关键
nd->right = CreateTree(i, r);
return nd;
}

void DelTree(Node **nd)
{
if (*nd == NULL) return;
DelTree(&(*nd)->left);
DelTree(&(*nd)->right);
delete *nd;
*nd = 0;
}
bool bIsTree = true;

int lastnum = -1;
void dfs(Node* nd, int cnt)
{
if (!bIsTree) return;//首次进入判断根节点颜色,若红色返回错误(1)
if (nd == NULL)
{//(5)
if (lastnum == -1) lastnum = cnt;//首次进入,记录到某一个叶节点上的黑色节点数目
else if (lastnum != cnt) { bIsTree = false; }//非首次进入,判断到其它节点的黑色节点是否等于基准值,若不等于,则不是红黑树
return;
}
if (nd->bBlack) ++cnt;
else
{//(4)
if (nd->left && !nd->left->bBlack) bIsTree = false;//如果红节点的子节点是黑节点,返回错误
if (nd->right && !nd->right->bBlack) bIsTree = false;//如果红节点的子节点是黑节点,返回错误
}
dfs(nd->left, cnt);
dfs(nd->right, cnt);

}

int main()
{
int t;
scanf("%d", &t);
while (t--)
{
scanf("%d", &n);
for (int i = 0; i<n; ++i)
scanf("%d", &a[i]);
Node* root = CreateTree(0, n - 1);
bIsTree = root->bBlack;
lastnum = -1; //初始化别忘了
dfs(root, 0);
if (bIsTree) printf("Yes\n");
else printf("No\n");
DelTree(&root); //清理内存也很重要,因为很多公司会看代码,这一行代码有加分。
}
return 0;
}
对我来说这道题算是很难了QQQAAAQQQ
附:关于指针
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: