本题考查
红黑树、树的深搜、树的广搜
思路
红黑树简介:https://zh.wikipedia.org/wiki/%E7%BA%A2%E9%BB%91%E6%A0%91
因为红黑树也是二叉搜索树,所以可以根据先序遍历顺序与元素相对大小构建树,然后使用BFS对每个元素进行判断,是否符合红黑树条件。
题目给了红黑树的五个条件:
- Every node is either red or black.
- The root is black.
- Every leaf (NULL) is black.
- If a node is red, then both its children are black.
- For each node, all simple paths from the node to descendant leaves contain the same number of black nodes.
第1、3不用判断,2,4在BFS过程中即可判断,5需要在BFS中对每个结点进行DFS,判断从该结点开始每条到null结点的路径的黑结点个数。
AC代码
import java.util.LinkedList;
import java.util.Scanner;
public class Main {
static int blackNum = -1;
static class Node {
int color;// 0代表红,1代表黑
int key;
Node leftChild;
Node rightChild;
public Node(int color, int key) {
this.color = color;
this.key = key;
this.leftChild = null;
this.rightChild = null;
}
}
static Node insert(Node root, int color, int key) {
if (root == null) root = new Node(color, key);
else if (key < root.key) root.leftChild = insert(root.leftChild, color, key);
else if (key > root.key) root.rightChild = insert(root.rightChild, color, key);
return root;
}
static boolean dfs(Node root, int num) {
boolean flag = true;
if(root == null) {
if(blackNum == -1) blackNum = num;
else if(blackNum != num) flag = false;
}
else {
if(root.color == 1) num++;
if(!dfs(root.leftChild, num)) flag = false;
if(!dfs(root.rightChild, num)) flag = false;
}
return flag;
}
static boolean bfs(Node root) {
LinkedList<Node> list = new LinkedList<Node>();
boolean flag = true;
if(root.color == 0) flag = false;
else {
list.add(root);
while(list.size() > 0) {
Node n = list.remove();
if(n.color == 0)
if((n.leftChild != null && n.leftChild.color == 0)|| (n.rightChild != null && n.rightChild .color == 0)) {
flag = false;
break;
}
blackNum = -1;
if(!dfs(n, 0)) { flag = false; break; }
if(n.leftChild != null) list.add(n.leftChild);
if(n.rightChild != null) list.add(n.rightChild);
}
}
return flag;
}
public static void main(String[] args) {
Scanner scaner = new Scanner(System.in);
int round = scaner.nextInt();
Node root = null;
for (int i = 0; i < round; i++) {
int num = scaner.nextInt();
root = null;
int j;
for (j = 0; j < num; j++) {
int temp = scaner.nextInt();
if (temp < 0) root = insert(root, 0, Math.abs(temp));
else root = insert(root, 1, Math.abs(temp));
}
if(bfs(root)) System.out.println("Yes");
else System.out.println("No");
}
scaner.close();
}
}
提醒自己
勿忘全局变量blackNum的还原,第49行